如何移动谷歌地图以及定期移动标记

时间:2016-08-30 14:05:27

标签: android google-maps android-layout google-maps-markers marker

我正在开发谷歌地图应用程序,其中我设置了谷歌地图和移动车辆的标记。我从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.

任何帮助都应该对我有很大帮助。

2 个答案:

答案 0 :(得分:3)

因此,为了将地图与标记位置更改一起移动,您可以执行以下操作:

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);
}

当然,您需要访问GoogleMap对象。

答案 1 :(得分: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上发现了一些关于谷歌地图的有趣课程: