Bhaumik Belani
Bhaumik Belani

Reputation: 671

OSMDROID: How to rotate OSM map as per device's direction?

I am using OSMDROID. Currently when I move around the map stays as it is(north direction remains at the top of my screen) but I want to rotate the map as per device's direction. e.g. if I move towards the East, the map rotates of right side and shows east on top of my device screen.

is there any solution for this?

Upvotes: 1

Views: 3071

Answers (1)

spy
spy

Reputation: 3258

There's an example in the sample application. Have you looked at it yet? It's not perfect and you'll want to supplement it with gps heading

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);

````

set up the rotation, taking into account device rotation

````

 @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);
}

````

Upvotes: 2

Related Questions