我使用谷歌地圖標記爲我當前的位置我能夠檢索當前的經度和緯度的位置,但我怎樣才能得到更新的位置,當它被更改。我編寫了一個跟蹤當前位置的單獨課程,並在主要Activity中調用課程,其中我使用googleApi客戶端來顯示標記,現在問題是如何在此主要活動課程中更改位置時如何更改標記OnLocation更改與谷歌地圖API在Android
0
A
回答
3
就像你正在跟蹤來自不同班級的您的位置。你可以創建自己的監聽器,說來聽聽在主類的位置變化
首先創建位置變化監聽
public interface LocationListener {
public void onLocationReceived(Location location);
}
在你的電話一樣,
public class MainActivity extends Activity implement LocationListener{
public void onLocationReceived(Location location){
// update your map pin here
}
}
實現這個監聽器調用您的位置時從mainactivity類通過位置更改的收聽者類似 那樣
LocationClass class = new LocationClass(this);
在LocationClass添加costructor
LocationListener listener;
public LocationClass (LocationListener listener){
this.listener = listener;
}
現在,每當你得到新的位置調用這個函數
this.listener.onLocationReceived(location);
它會在你的情況下工作。
您有其他的選擇一樣可以實現谷歌的位置監聽器在自己的活動
0
@ user1335906您可以參考以下鏈接以獲得頻繁的位置更新...
https://github.com/googlesamples/android-play-location/tree/master/LocationUpdates
1)首先您必須使用以下方法連接Google服務:
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
2)成功連接到Google服務後,爲您的locationRequest對象設置了usi下面納克法:
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1500); // this will give location update after 1500 miliseconds, set it according to your requirement
mLocationRequest.setSmallestDisplacement(0.5f); // this will calculate distance between your previous and current location, if it is greater then 0.5 meter this method will be triggered.
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); // set location accuracy level as per app requirement.
}
2)您將獲得以下方法頻繁的位置更新:
public void onLocationChanged(Location location) {
// Here you will get your current location every time when it gets changed,
// put log here and see current location updates
}
所以給它一個嘗試,讓我知道,如果你沒有得到它...
0
通常,當您添加標記時,您可以參考標記。
Marker marker = mGoogleMap.addMarker(...)
所以,當你收到的位置變化,則只是單純地改變它的位置
marker.setPosition(new LatLng(...));
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(...));
這將動畫地圖移動到的位置變化,但不會隨着標記動畫。如果距離很遠,標記從一個地方跳到另一個地方。所以,如果你想動畫標記,那麼使用ValueAnimator動畫標記。
相關問題
- 1. Android谷歌地圖API與JavaScript谷歌地圖API
- 2. 谷歌地圖移動SDK商業與谷歌地圖Android API
- 3. Android IllegalStateException與谷歌地圖api v2
- 4. 谷歌地圖API v2。 android
- 5. 谷歌地圖API的Android
- 6. Android谷歌地圖API v2
- 7. 谷歌地圖Android API v2
- 8. Android |谷歌地圖API
- 9. Android谷歌地圖api
- 10. 的Android +谷歌地圖API
- 11. Android Studio谷歌地圖API
- 12. 谷歌地圖android api autocomplete
- 13. 與谷歌地圖API
- 14. 與谷歌地圖API
- 15. 更改Polyline谷歌地圖android
- 16. 谷歌地圖的Android更改界限
- 17. 在谷歌地圖v3中更改谷歌地圖中心
- 18. 更改谷歌地圖
- 19. IOS地圖(谷歌地圖API與MapKit?)
- 20. 谷歌地圖與Android
- 21. 谷歌地圖與Android
- 22. 谷歌地圖Android API v2動畫谷歌地圖查看
- 23. 在谷歌地圖Android API中使用谷歌地圖離線地圖(緩存)
- 24. 谷歌地圖api key`在android中
- 25. 谷歌地圖Android API V2顯示當前位置與標記(我在谷歌Android地圖API第二版)
- 26. 更改地圖邊界谷歌地圖API
- 27. 谷歌地圖API V3 - 更改地圖的div
- 28. 谷歌地圖api與地名
- 29. 谷歌地圖API
- 30. 谷歌地圖API
分享您的代碼。 – Piyush
你到目前爲止嘗試過什麼? –
您設置了requestLocationChange監聽器和onLocationChange,您可以更新您的標記。 – Meenal