Android Maps v2 rotate mapView with compass

20,347

Solution 1

OK, i figured it out myself. first you need to calculate the bearing from the compass. then the Maps api-2 camera can be rotated.

public void updateCamera(float bearing) {
            CameraPosition currentPlace = new CameraPosition.Builder()
                    .target(new LatLng(centerLatitude, centerLongitude))
                    .bearing(bearing).tilt(65.5f).zoom(18f).build();
            googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(currentPlace));

    }

set SensorListener in your code and call this method in onSensorChanged event. i have added a tilt value so the map will rotate in 3D.

Solution 2

in your GoogleMap object you can access getMyLocation method. This last returns a Location object which contains getBearing method. This one returns a float [0..360] computed from last known location and current location, 0° is the north axis, rotation is in the clock sens.

To resume, you can use a code like:

GoogleMap gMap = ..... 
float bearing = gMap.getMyLocation().getBearing();
CameraPosition newCamPos = new CameraPosition(latLngObject,
            zoomValue,
            tiltValue,
            bearing);
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(newCamPos), durationValue, null);
Share:
20,347
Asanka Senavirathna
Author by

Asanka Senavirathna

Updated on March 07, 2020

Comments

  • Asanka Senavirathna
    Asanka Senavirathna about 4 years

    I'm developing an app which needs to rotate the mapView with a compass. I know how to rotate the camera but I need to rotate the mapView with the compass. the center point should be the current location. I found codes for Maps V1 but I need to do it with Maps V2

  • Chrispix
    Chrispix about 11 years
    Not that it matters too much, but .bearing is a float.
  • sealskej
    sealskej over 10 years
    How do you get bearing?
  • Joubert Vasconcelos
    Joubert Vasconcelos about 10 years
    If you are using GoogleMap instead of MapView, you can use the mMap.setOnMyLocationChangeListener event.
  • Nah
    Nah over 6 years
    How can we do this in V3? Please suggest. Also, I have posted similar question in order to rotate map 360 Degree by changing bearing 3 times (each time by 120 Degree to rotate 1 cycle around a center point) but there are easing and jerks. How can I avoid that. stackoverflow.com/questions/48123738/…
  • Nah
    Nah over 6 years
    How can we do this in V3? Please suggest. Also I have posted similar question in order to rotate map 360 Degree by changing bearing 3 times (each time by 120 Degree to rotate 1 cycle around a center point) but there are easing and jerks. How can I avoid that. stackoverflow.com/questions/48123738/…
  • lostintranslation
    lostintranslation over 5 years
    Sure would be nice if you would show how you got the bearing, pretty important piece if information.
  • Nas
    Nas over 5 years
    You can get bearing value form the Location (Current location) return by the location provider.