0

我正在研究谷歌地圖應用程序,其中我已設置谷歌地圖和移動車輛的標記。我從API獲取經緯度,並且定期更新谷歌地圖上的標記(每25秒)。如何將谷歌地圖與定期移動的標記一起移動

  When updating marker I am unable to move the google map along with the marker. How to move the google map along with the periodically moving marker. 

任何幫助對我來說應該是一個很大的幫助。

回答

1

下面的代碼片段說明一些常見的方式移動攝像機:

private static final LatLng SYDNEY = new LatLng(-33.88,151.21); 
private static final LatLng MOUNTAIN_VIEW = new LatLng(37.4, -122.1); 

private GoogleMap map; 
... 

// Obtain the map from a MapFragment or MapView. 

// Move the camera instantly to Sydney with a zoom of 15. 
map.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 15)); 

// Zoom in, animating the camera. 
map.animateCamera(CameraUpdateFactory.zoomIn()); 

// Zoom out to zoom level 10, animating with a duration of 2 seconds. 
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 

// Construct a CameraPosition focusing on Mountain View and animate the camera to that position. 
CameraPosition cameraPosition = new CameraPosition.Builder() 
       .target(MOUNTAIN_VIEW)      // Sets the center of the map to Mountain View 
       .zoom(17)                   // Sets the zoom 
       .bearing(90)                // Sets the orientation of the camera to east 
       .tilt(30)                   // Sets the tilt of the camera to 30 degrees 
       .build();                   // Creates a CameraPosition from the builder 

map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

基礎上Documentation for the Android Google Maps API

編輯:

我發現了關於谷歌地圖上Udacity一些有趣的課程:

+1

感謝George.SP爲此代碼。這是工作。 –

2

因此,爲了與標記位置的變化而移動地圖,你可以做水木清華這樣的:

public void onLocationChanged(Location loc) { 
    //some marker movements here... 

    CameraPosition currentPlace = new CameraPosition.Builder() 
      .target(new LatLng(loc.getLatitude(), loc.getLongitude()) 
      .tilt(0f) 
      .zoom(18) 
      .build(); 

    map.animateCamera(CameraUpdateFactory.newCameraPosition(currentPlace), 200, null); 
} 

Ofcourse您需要訪問GoogleMap對象。

+0

感謝Rybzor該代碼段。這很有幫助。 –

相關問題