2011-08-29 24 views
1

我在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分鐘,直到位置將繪製在地圖上。

我不知道問題在哪裏。 感謝您的幫助!

+0

這可能不會回答你的問題,但我知道獲得[真實]修復有時需要幾分鐘。我最近的應用程序使用GPS,它很令人沮喪,因爲我知道我什麼時候開始它,如果最近我沒有得到修復,它將需要幾分鐘。 – Jack

+1

mMyLocationOverlay.runOnFirstFix(新的Runnable(){ @Override 公共無效的run(){ mMapView.getController()animateTo( mMyLocationOverlay.getMyLocation());} });而不是這個。並寫入mMapView.getController()。animateTo( mMyLocationOverlay.getMyLocation());嘗試一下。 – user370305

+0

Hello @Jack感謝您的回答。但是,我現在有點困惑。就像在日誌文件中看到的那樣,只有4秒鐘,直到應用程序捕獲一個位置。 – Chris

回答

1

MyLocationOverlay是Google Maps for Android的幫助類。這是它自己的位置獲取,更具體地說,它使用GPS來做到這一點。

由於您致電runOnFirstFix(runnable),它在執行代碼之前會等待GPS修復程序。有時候(尤其是室內)需要幾分鐘時間。因此延遲。它也完全有可能獲得基於網絡的位置,但不是GPS位置(尤其是在室內),所以在這種情況下,您的代碼將永遠不會執行(除非MyLocationOverlay在超時後回落到網絡提供商處 - 這不在任何地方解釋)。

您在日誌中看到的位置是您通過網絡位置提供商(wifi和小區網絡位置)獲取的位置。

只需刪除runOnFirstFix() as @ user370305在評論中提示。

+0

你好彼得和@ user370305。非常感謝我們的答案。我刪除runOnFirstFix()並設置MapController.animateTo(GeoPoint)的初始位置後,它運行非常平穩:)。 – Chris

相關問題