Android地图自动旋转地图、Android

由网友(单名一个晗)分享简介:如果你打开​​谷歌地图应用程序,没有对,你可以preSS来围绕​​您的当前位置在地图画面右上方的按钮。按钮的图标,然后改变。如果再次preSS相同的按钮,根据您的罗盘航向地图上自动旋转。换句话说,地图变得自我中心(相对于非自我中心AKA北总是向上​​)。If you open the Google maps app,...

如果你打开​​谷歌地图应用程序,没有对,你可以preSS来围绕​​您的当前位置在地图画面右上方的按钮。按钮的图标,然后改变。如果再次preSS相同的按钮,根据您的罗盘航向地图上自动旋转。换句话说,地图变得自我中心(相对于非自我中心AKA北总是向上​​)。

If you open the Google maps app, there is a button on the top right of the screen that you can press to center the map on your current location. The button's icon then changes. If you press the same button again, the map auto-rotates based on your compass heading. In other words, the map becomes egocentric (as opposed to allocentric AKA north always up).

谷歌最近推出地图API V2为Android,我当然喜欢它比旧的。默认情况下,Android地图V2将包括按钮,在上定位中心。然而,pressing不止一次不会启用自动旋转;它只是试图再次集中在你的位置在地图上。

Google recently launched maps API V2 for Android and I certainly like it more than the old one. By default, android maps V2 will include the "center on location" button. However, pressing it more than once does not enable auto-rotation; it merely tries to center the map on your location again.

有谁知道我可以自动旋转,使用地图API V2就像谷歌地图应用程序做的地图?请问我要实现这个功能我自己还是在API中,我只是没有看到呢?我AP preciate所有帮助。

Does anyone know how I can auto-rotate the map using maps API v2 just like the google maps app does? Will I have to implement this functionality myself or is it in the API and i'm just not seeing it? I appreciate all help.

推荐答案

确定这里就是我想应该是在一年后完成。请纠正我,如果你发现任何问题。

Ok here's how I think it should be done a year later. Please correct me if you spot any issues.

下面的大多数code处理的坐标系之间的差异。我使用的是旋转矢量传感器。从文档: Y是切到设备的当前位置和点指向磁北地轴承在谷歌地图,而另一方面,似乎指向正北。 。 this页面显示的转换是如何完成的

Most of the following code deals with a discrepancy between coordinate systems. I'm using a rotation vector sensor. From the docs: Y is tangential to the ground at the device's current location and points towards magnetic north. Bearing in google maps, on the other hand, seems to point to true north. this page shows how the conversion is done

1),从当前GPS位置获得当前的偏角

1) get the current declination from your current GPS location

@Override
public void onLocationChanged(Location location) {
    GeomagneticField field = new GeomagneticField(
            (float)location.getLatitude(),
            (float)location.getLongitude(),
            (float)location.getAltitude(),
            System.currentTimeMillis()
        );

    // getDeclination returns degrees
    mDeclination = field.getDeclination();
} 

2)计算的偏角和磁北轴承

2) calculate bearing from declination and magnetic north

    @Override
public void onSensorChanged(SensorEvent event) {
    if(event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
        SensorManager.getRotationMatrixFromVector(
                mRotationMatrix , event.values);
        float[] orientation = new float[3];
        SensorManager.getOrientation(mRotationMatrix, orientation);
        float bearing = Math.toDegrees(orientation[0]) + mDeclination;
        updateCamera(bearing);  
    }
}

3)更新地图

3) update maps

private void updateCamera(float bearing) {
    CameraPosition oldPos = mMap.getCameraPosition();

    CameraPosition pos = CameraPosition.builder(oldPos).bearing(bearing).build();
            mMap.moveCamera(CameraUpdateFactory.newCameraPosition(pos));
}
阅读全文

相关推荐

最新文章