开发者

Android : Location Update in Mapview takes to long even tough Location is found

开发者 https://www.devze.com 2023-03-31 06:27 出处:网络
I have a problem with my Location Listener in a MapView Application. Especially there is a Problem with the time, my actual Position will be painted on a Map.

I have a problem with my Location Listener in a MapView Application. Especially there is a Problem with the time, my actual Position will be painted on a Map.

I attached two listeners to my MapView. The first one waits for a signal from the Network and the second one from the GPS. At the beginning of the Listener Implementation I get the Last Known Location to reduce the Time while other providers search a position.

My LocationListner in the MapView is called as followed:

public void locationManager(){

    mLocationUpdateHandlerGPS       = new LocationUpdateHandler();
    mLocationUpdateHandlerNetwork   = new LocationUpdateHandler();

    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    //-------Check if Network is available-----
    mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationUpdateHandlerNetwork);

    //-------Check if GPS is available---------
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, mLocationUpdateHandlerGPS);


}

The LocationUpdateHandler.class where i implemented the Listener looks like this:

public class LocationUpdateHandler implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {

        location = mLocationManager开发者_JAVA技巧.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        int lat = (int) (location.getLatitude() * 1E6);
        int lng = (int) (location.getLongitude() * 1E6);

        //Log Actual Position
        Log.v(TAG, "Actual Postion // Get Latitude: " + lat);           
        Log.v(TAG, "Actual Postion // Get Longitude: " + lng);

        mMyLocationOverlay.enableMyLocation();
        Log.v(TAG, "Enable Location returns: "+mMyLocationOverlay.enableMyLocation());

        mMyLocationOverlay.runOnFirstFix(new Runnable() {
            @Override
            public void run() {
                mMapView.getController().animateTo(
                        mMyLocationOverlay.getMyLocation());
            }
        });
    }

}

After the Map starts i check the Log Output from the DDMS and can see that the Location is get in Time.

08-29 14:50:16.136: VERBOSE/GeoTagMapActivity(7300): Running Activity GeoTagMapActivity
08-29 14:50:20.691: VERBOSE/GeoTagMapActivity(7300): Enable Location returns: true
08-29 14:50:20.691: VERBOSE/GeoTagMapActivity(7300): Actual Postion // Get Latitude: *******
08-29 14:50:20.691: VERBOSE/GeoTagMapActivity(7300): Actual Postion // Get Longitude: *******

Nevertheless i takes another 2 - 5 minutes until the Position will drawn on the Map.

I have no idea where's the Problem. Thanks for your help!


MyLocationOverlay is a helper class of Google Maps for Android. It does it's own location acquiring, more specifically it uses GPS to do it.

Since you call runOnFirstFix(runnable) it waits to get a GPS fix before executing your code. This can sometimes (especially indoor) take minutes. Hence the delay. It's also entirely possible to acquire network-based location, but not GPS position (especially indoors), so in this case your code would never be executed (except if MyLocationOverlay falls back to network provider after timeout - this isn't explained anywhere).

The position that you see in the logs are positions that you acquire via network location provider (wifi and cell network locations).

Just remove runOnFirstFix() as @user370305 suggested in comments.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号