BoyUnderTheMoon
BoyUnderTheMoon

Reputation: 771

Android Google Maps current location bearing

I'm currently using Google Maps on Android, but I'm not sure how to rotate the map to show the users current facing direction.

When I set:

mMap.setMyLocationEnabled(true);

I notice that the current marker has a small bearing arrow (when phone is tilted, etc).

@Override
public void onLocationChanged(Location location) {
    handleNewLocation(location);
}

private void handleNewLocation(Location location) {
    Log.d(TAG, location.toString());

    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();

    LatLng latLng = new LatLng(currentLatitude, currentLongitude);

    CameraPosition camPos = new CameraPosition.Builder()
            .target(latLng)
            .zoom(16.0f)
            .bearing(location.getBearing())
            .build();

    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos));
}

This successfully updates the markers position on the map based on locations. However, how can I change the facing direction of the marker by rotating the map, based on the phones yaw tilting.

Upvotes: 2

Views: 3006

Answers (1)

Rahim Rahimov
Rahim Rahimov

Reputation: 1417

Try this code:

private SensorManager mSensorManager;
private SensorEventListener sensorEventListener;
private Sensor accelerometer;
private Sensor magnetometer;
private float[] mGravity;
private float[] mGeomagnetic;
private Float azimut;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);

    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    sensorEventListener= new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {

            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
                mGravity = event.values;
            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];
                boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
                if (success) {
                    float orientation[] = new float[3];
                    SensorManager.getOrientation(R, orientation);
                    azimut = orientation[0]; // orientation contains: azimut, pitch and roll
                    float degrees = (float) Math.toDegrees(azimut);
                    /**
                     * TRY THIS TO UPDATE YOUR CAMERA
                     * degrees you can use as bearing
                     * CameraPosition cameraPosition = new CameraPosition( myLatLng, 15, 0, degrees);
                     * map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition),200, null);
                     **/
                }
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };
}

@Override
protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(sensorEventListener);
}

@Override
protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(sensorEventListener, accelerometer, SensorManager.SENSOR_DELAY_UI);
    mSensorManager.registerListener(sensorEventListener, magnetometer, SensorManager.SENSOR_DELAY_UI);
}

Upvotes: 4

Related Questions