Naveen Dissanayake
Naveen Dissanayake

Reputation: 672

device orientation change resets the bearing calculation output

I'm trying to set map bearing according to device orientation. So far it's working fine, but occasionally the bearing gets set to 0. I have checked by logging my bearing calculation output and the bearing I get through OnCameraChangeListener, it seems my calculation is correct but somehow it resets to 0 inside moveCamera() method. The funny thing is it seems to be occurring at a regular interval (around 5-6 seconds). Has anyone else encountered this issue? Is there a reason for this? Is there a workaround? Thanks in advance.

EDIT Here is the code I used for rotating the map.

private void init() {
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        mGravity = event.values;
    } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        mGeomagnetic = event.values;
    }

    if(mGravity != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];
        float newBearing;
        boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
        if (success) {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            if (mBearing != Float.MIN_VALUE) {
                newBearing = mBearing
                        + ALPHA
                        * ((float) Math.toDegrees(orientation[0]) - mBearing);
            } else {
                newBearing = ((float) Math.toDegrees(orientation[0]));
            }
            mBearing = newBearing;
            CameraPosition cameraPosition = CameraPosition
                    .builder(mMap.getCameraPosition())
                    .bearing(mBearing).build();
            CameraUpdate cameraUpdate = CameraUpdateFactory
                    .newCameraPosition(cameraPosition);
            mMap.moveCamera(cameraUpdate);
        }
     }
 }

Upvotes: 0

Views: 72

Answers (1)

Cory Roy
Cory Roy

Reputation: 5619

When the phone is rotated, Android will destroy the activity and rebuild it losing any ephemeral state that you haven't saved and restored. If you wish to keep android from doing that you can tell it to not rebuild your activity on configuration changes.

   <?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
       ...
       <application
          ...
          <activity
              android:name="<YourActivityName>"
              android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
              ...
           />
       />
    />

There are a number of events that will cause a configuration change and a rebuild of your activity. That's why keyboard and screenSize are included in that list.

Upvotes: 1

Related Questions