Search code examples
androiddictionaryosmdroidcompass-geolocation

osmdroid get orientation from Compass [How to]


I use Osmdroid in my map app.

I want to get radian(oriantation) of compass for rotate map when click compass overlay.

eg. press compass icon then update orientation map true direction.

i know method setMapOrientation for rotate map thank u.


Solution

  • You can use internal compass of the device and use SensorListener in android. Then use MapOrientation method to rotate the osmmap.

    Some snippet:

    mSensorManager.registerListener(mySensorEventListener,
                            SensorManager.SENSOR_ORIENTATION,
                            SensorManager.SENSOR_DELAY_UI);
    
    public SensorListener mySensorEventListener = new SensorListener(){
    
            @Override
            public void onSensorChanged(int sensor, float[] values) {
                synchronized (this) {
                    float mHeading = values[0];             
                    mOSMView.setMapOrientation(-mHeading);
                }
            }
        };