android OSMDROID:如何根据设备的方向旋转OSMMap?

mitkmikd  于 2022-11-20  发布在  Android
关注(0)|答案(1)|浏览(166)

我使用的是OSMDROID。目前,当我四处移动时,Map保持不变(北方方向保持在屏幕顶部),但我想根据设备的方向旋转Map。例如,如果我向东移动,Map将旋转到右侧,并在设备屏幕顶部显示东方。
有什么解决办法吗?

92dk7w1h

92dk7w1h1#

在示例应用程序中有一个例子。你看过了吗?它并不完美,你会想用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);
}
```

相关问题