我正在使用MyLocationOverlay來顯示當前位置。但是,該位置需要一段時間才能更新。我想實現類似iPhone上的「跟隨我」功能,其中用戶的位置也隨着用戶移動而移動。如何讓MyLocationOverlay的標記在其位置上執行實時更新?如何實現用戶當前位置的實時更新?
請注意,我重寫draw()和drawMyLocation(),因爲我需要繪製自定義標記。
我現在的代碼看起來像這樣:(我的代碼的問題是標記不隨用戶移動,因爲調用onLocationChanged之前需要一段時間,標記會突然從一個地方跳到另一個地方。)
@Override
public synchronized void onLocationChanged(Location location)
{
if (location != null && mapController != null)
{
//Recenter map based on current location
mapController.animateTo(new GeoPoint((int) (location.getLatitude() * 1e6), (int) (location.getLongitude() * 1e6)));
}
super.onLocationChanged(location);
}
謝謝!我今天再次測試了我的應用程序,它的工作比昨天更好。雖然有時候標記仍會從一個地方跳到另一個地方。我沒有改變我的代碼,所以看起來我昨天剛好位置不好。我沒有直接使用LocationManager,因爲我只是通過擴展它來重用MyLocationOverlay類。我應該創建一個LocationManager嗎?順便說一下,你知道MyLocationOverlay多久更新一次嗎? – Arci 2012-02-08 02:42:56
給我提供虛假的位置更新也出現在我的腦海中,但我決定不實施它,因爲可能會產生很多問題,比如用戶突然改變他的方向,或者如果他正在朝西東等我檢查了getTime方法。我以前沒有注意到它。謝謝你的提示! – Arci 2012-02-08 02:46:05
您不必在此創建LocationManager :):http://developer.android.com/guide/topics/location/obtaining-user-location.html是如何獲取它的示例,但您不需要需要它,只要你使用MyLocationOverlay - 有啓用/禁用myLocation()方法已經在內部做。您無法設置MyLocationOverlay更新的頻率(請參閱http://stackoverflow.com/questions/8394867/how-do-you-implement-follow-me-using-the-mylocationoverlay-class),所以如果您想要完全控制你必須編寫自己的覆蓋和聽衆的頻率。 – 2012-02-08 07:12:34