Android:即使找到困难的位置,Mapview 中的位置更新也需要很长时间

发布于 2024-12-02 02:08:01 字数 2281 浏览 1 评论 0原文

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

我将两个侦听器附加到我的 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());
            }
        });
    }

}

地图启动后,我检查 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 分钟才能在地图上绘制位置。

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

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.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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝戈者 2024-12-09 02:08:01

MyLocationOverlay< /code>是 Android 版 Google 地图的辅助类。它自己进行位置获取,更具体地说,它使用 GPS 来获取位置。

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

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

只需按照 @user370305 在评论中建议的那样删除 runOnFirstFix() 即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文