我正在开发一个需要用指南针旋转mapView的应用程序。我知道如何旋转相机,但我需要用指南针旋转mapView。中心点应该是当前位置。我找到了地图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对象中,您可以访问getMyLocation方法。这最后返回一个包含getBearing方法的Location对象。这个返回从最后已知位置和当前位置计算的浮点[0..360],0°是北轴,旋转是时钟sens。
要恢复,您可以使用以下代码:
GoogleMap gMap = .....
float bearing = gMap.getMyLocation().getBearing();
CameraPosition newCamPos = new CameraPosition(latLngObject,
zoomValue,
tiltValue,
bearing);
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(newCamPos), durationValue, null);