Android Maps v2用指南针旋转mapView

问题描述 投票:9回答:2

我正在开发一个需要用指南针旋转mapView的应用程序。我知道如何旋转相机,但我需要用指南针旋转mapView。中心点应该是当前位置。我找到了地图V1的代码,但我需要使用Maps V2

android google-maps google-maps-api-2 google-maps-android-api-2 android-maps-v2
2个回答
19
投票

好吧,我自己想通了。首先,您需要从罗盘计算轴承。然后可以旋转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旋转。


3
投票

在您的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);
© www.soinside.com 2019 - 2024. All rights reserved.