【发布时间】:2013-01-23 21:45:46
【问题描述】:
我正在开发一个需要用指南针旋转 mapView 的应用程序。我知道如何旋转相机,但我需要用指南针旋转 mapView。中心点应该是当前位置。我找到了 Maps V1 的代码,但我需要使用 Maps V2 来完成
【问题讨论】:
标签: android google-maps google-maps-api-2 google-maps-android-api-2 android-maps-v2
我正在开发一个需要用指南针旋转 mapView 的应用程序。我知道如何旋转相机,但我需要用指南针旋转 mapView。中心点应该是当前位置。我找到了 Maps V1 的代码,但我需要使用 Maps V2 来完成
【问题讨论】:
标签: android google-maps google-maps-api-2 google-maps-android-api-2 android-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° 是北轴,旋转是时钟感应。
要恢复,您可以使用如下代码:
GoogleMap gMap = .....
float bearing = gMap.getMyLocation().getBearing();
CameraPosition newCamPos = new CameraPosition(latLngObject,
zoomValue,
tiltValue,
bearing);
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(newCamPos), durationValue, null);
【讨论】: