我正在开发一个需要使用指南针旋转地图视图的应用程序。我知道如何旋转摄像机,但我需要用指南针旋转地图视图,并以当前位置为中心点。我找到了Maps V1的代码,但我需要在Maps V2中实现它。
好的,我自己解决了。首先需要从指南针计算方位角,然后可以旋转Maps api-2相机。
public void updateCamera(float bearing) {
CameraPosition currentPlace = new CameraPosition.Builder()
.target(new LatLng(centerLatitude, centerLongitude))
.bearing(bearing).tilt(65.5f).zoom(18f).build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(currentPlace));
}
在你的代码中设置SensorListener
,并在onSensorChanged
事件中调用此方法。我已经添加了倾斜值,因此地图将以3D形式旋转。
GoogleMap gMap = .....
float bearing = gMap.getMyLocation().getBearing();
CameraPosition newCamPos = new CameraPosition(latLngObject,
zoomValue,
tiltValue,
bearing);
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(newCamPos), durationValue, null);