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

标签 android geolocation gps location locationlistener

我在 MapView 应用程序中的位置监听器有问题。 特别是时间有问题,我的实际位置将被绘制在 map 上。

我将两个监听器附加到我的 MapView。第一个等待来自网络的信号,第二个等待来自 GPS 的信号。在监听器实现开始时,我获得了最后已知位置以减少其他提供商搜索位置的时间。

我在 MapView 中的 LocationListner 调用如下:

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);


}

我实现监听器的 LocationUpdateHandler.class 如下所示:

public class LocationUpdateHandler implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {

        location = mLocationManager.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());
            }
        });
    }

}

map 启动后,我检查了 DDMS 的日志输出,可以看到位置是及时获取的。

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: *******

不过,我还需要 2 - 5 分钟才能在 map 上绘制位置。

我不知道问题出在哪里。 感谢您的帮助!

最佳答案

MyLocationOverlay是 Google Maps for Android 的辅助类。它执行自己的位置获取,更具体地说,它使用 GPS 来执行此操作。

由于您调用了 runOnFirstFix(runnable),它会在执行您的代码之前等待获取 GPS 定位。这有时(尤其是在室内)需要几分钟时间。因此延迟。也完全有可能获取基于网络的位置,但不是 GPS 位置(尤其是在室内),因此在这种情况下,您的代码将永远不会执行(除非 MyLocationOverlay 在超时后回退到网络提供商 - 这任何地方都没有解释)。

您在日志中看到的位置是您通过网络位置提供商(wifi 和手机网络位置)获取的位置。

只需删除 runOnFirstFix() 作为@user370305 在评论中的建议。

关于安卓 : Location Update in Mapview takes to long even tough Location is found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7230712/

相关文章:

java - 在简单的圆柱投影上将纬度/经度转换为图像坐标(像素坐标)

android - 如何在没有播放服务的情况下让 Android 定位工作?

java - Error :(124, 62)错误:不兼容的类型:无法将类转换为上下文

android - 每行的标题在 android 4.1 收件箱样式通知中应该看起来不同吗?

Android OpenGL 点云

javascript - 如何使用 slimerJS 允许访问浏览器地理定位 API?

swift - 如何正确获取用户坐标?

javascript - Web 应用程序中的地理位置欺骗保护

mobile - 如何使用纯短信确定用户/手机的位置?

android - 在项目上单击监听器以扩展 ArrayAdapter 的 ListView