我在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