OSMDROID:如何根据设备的方向旋转OSM地图?

时间:2016-06-01 17:46:10

标签: android openstreetmap osmdroid

我正在使用OSMDROID。目前当我在地图上移动时保持原样(北方向保持在我的屏幕顶部),但我想按照设备的方向旋转地图。例如如果我向东移动,地图将向右旋转并向东显示在我的设备屏幕上。

有没有解决方案?

1 个答案:

答案 0 :(得分:2)

示例应用程序中有一个示例。你看过了吗?它并不完美,你想用gps标题来补充它

https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/samplefragments/SampleHeadingCompassUp.java

````

    //lock the device in current screen orientation
    int orientation = getActivity().getRequestedOrientation();
    int rotation = ((WindowManager) getActivity().getSystemService(
            Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
    switch (rotation) {
        case Surface.ROTATION_0:
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            this.deviceOrientation=0;
            break;
        case Surface.ROTATION_90:
            this.deviceOrientation=90;
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            break;
        case Surface.ROTATION_180:
            this.deviceOrientation=180;
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
            break;
        default:
            this.deviceOrientation=270;
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
            break;
    }

    getActivity().setRequestedOrientation(orientation);


    LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    try {
        //on API15 AVDs,network provider fails. no idea why
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, (LocationListener) this);
    } catch (Exception ex) {
    }
    compass = new InternalCompassOrientationProvider(getActivity());
    compass.startOrientationProvider(this);
    mMapView.getController().zoomTo(18);

````

设置旋转,考虑设备旋转

````

 @Override
public void onOrientationChanged(float orientation, IOrientationProvider source) {
    //System.out.println("compass " + orientation);
    //System.out.println("deviceOrientation " + deviceOrientation);
    //this part adjusts the desired map rotation based on device orientation and compass heading
    float t=(360-orientation-this.deviceOrientation);
    if (t < 0)
        t+=360;
    if (t > 360)
        t-=360;
    //System.out.println("screen heading to " + t);
    mMapView.setMapOrientation(t);
}

````