我在MapView应用程序中的位置侦听器出现问题。 特别是时间有问题,我的实际位置将被绘制在地图上。Android:在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分钟,直到位置将绘制在地图上。
我不知道问题在哪里。 感谢您的帮助!
这可能不会回答你的问题,但我知道获得[真实]修复有时需要几分钟。我最近的应用程序使用GPS,它很令人沮丧,因为我知道我什么时候开始它,如果最近我没有得到修复,它将需要几分钟。 – Jack
mMyLocationOverlay.runOnFirstFix(新的Runnable(){ @Override 公共无效的run(){ mMapView.getController()animateTo( mMyLocationOverlay.getMyLocation());} });而不是这个。并写入mMapView.getController()。animateTo( mMyLocationOverlay.getMyLocation());尝试一下。 – user370305
Hello @Jack感谢您的回答。但是,我现在有点困惑。就像在日志文件中看到的那样,只有4秒钟,直到应用程序捕获一个位置。 – Chris