need a BETTER way for current location

纵然是瞬间 提交于 2019-12-25 01:10:28

问题


Through nearly all problems/answers here and on the web, the best way for current location is as follows:

if ( mLocationManager==null )
    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

Location l;
for ( String provider : mLocationManager.getAllProviders() ){
    mLocationManager.requestLocationUpdates(provider, 1000, 1, locationListener);
    l = mLocationManager.getLastKnownLocation(provider);
    if ( isBetterLocation(l, currentBestLocation) )
    currentBestLocation = l;
}

normally it works fine on Emulator and devices, but as noted by some guys, Android 4.0+ this code gives an exception:

09-06 22:46:16.163: ERROR/AndroidRuntime(351): Caused by: java.lang.IllegalArgumentException: provider=network
09-06 22:46:16.163: ERROR/AndroidRuntime(351):     at android.os.Parcel.readException(Parcel.java:1325)
09-06 22:46:16.163: ERROR/AndroidRuntime(351):     at android.os.Parcel.readException(Parcel.java:1275)
09-06 22:46:16.163: ERROR/AndroidRuntime(351):     at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646)
09-06 22:46:16.163: ERROR/AndroidRuntime(351):     at android.location.LocationManager._requestLocationUpdates(LocationManager.java:582)
09-06 22:46:16.163: ERROR/AndroidRuntime(351):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)

But other apps on the same device can work properly when there IS only WIFI/3G coverage(GPS not working indoors).

So my question is: How to retrieve current location inside a building?

PS: waiting for an Android image update is not acceptable, since other apps work properly on the same device. There MUST be a way to get the current location on the Android image with bug in LocationManager.

Kindest regards.

EDIT:

This time I tweaked my code as simple as possible(in the onResume):

    if ( mLocationManager==null )
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    for ( String provider : mLocationManager.getAllProviders() )
        mLocationManager.requestLocationUpdates(provider, 0, 0, locationListener);

and in the locationListener.onLocationChanged:

public void onLocationChanged(Location location) {
    if (isBetterLocation(location, currentBestLocation)) {
        currentBestLocation = location;
        ...make use of this location
    }
}

Only passive and gps providers are available, no network provider can be obtained on my XT928, while on the other phone it shows up.

EDIT 2: at last I achieved this by turning to a third-party library named as Baidu Location Library which also needs Access Key application. It works great.


回答1:


Try this: GoogleMap googleMap;

googleMap.setMyLocationEnabled(true);//Inside on create()

May this help you!!!




回答2:


Why do you call mLocationManager.requestLocationUpdates(provider, 1000, 1, locationListener); before mLocationManager.getLastKnownLocation(provider);? As far as I know, requestLocationUpdates will register a listener that will keep notifying you of the phone's location. getLastKnownLocation() will give you the last known location for the provider. So the two are to accomplish different things.

If you just want one instance of the current location, get rid of requestLocationUpdates(). Then, you need to check if the current best location as per your code above is good enough. If it is not, you can request a single update later (outside your for loop): locationManager.requestSingleUpdate(criteria, singleUpdatePI);

Also note that you can only get location from the providers if the user has authorized so in the settings. In Android 4+, "Location access" settings explicitly request for authorization to:

Let apps use Google's location service to estimate your location faster.



来源:https://stackoverflow.com/questions/20041898/need-a-better-way-for-current-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!