【问题标题】:Android Maps v2 rotate mapView with compassAndroid Maps v2 使用指南针旋转 mapView
【发布时间】: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


    【解决方案1】:

    好的,我自己想通了。首先,您需要从指南针计算方位。 然后可以旋转 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 旋转。

    【讨论】:

    • 并不是说它太重要了,但是 .bearing 是一个浮点数。
    • 你是怎么得到方位的?
    • 如果您使用的是 GoogleMap 而不是 MapView,您可以使用 mMap.setOnMyLocationChangeListener 事件。
    • 我们如何在 V3 中做到这一点?请建议。另外,我发布了类似的问题,以便通过改变方位 3 次(每次 120 度以围绕中心点旋转 1 个周期)来旋转地图 360 度,但存在缓动和抽搐。我怎样才能避免这种情况。 *.com/questions/48123738/…
    • 如果您能展示一下您是如何获得轴承的,那当然会很好,如果信息非常重要。
    【解决方案2】:

    在您的 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);
    

    【讨论】:

    • 我们如何在 V3 中做到这一点?请建议。我也发布了类似的问题,以便通过改变轴承 3 次(每次 120 度围绕中心点旋转 1 个周期)来旋转地图 360 度,但是有缓动和抽搐。我怎样才能避免这种情况。 *.com/questions/48123738/…