Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Maps v2 rotate mapView with compass

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

like image 348
Asanka Senavirathna Avatar asked Feb 08 '13 06:02

Asanka Senavirathna


2 Answers

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.

like image 185
Asanka Senavirathna Avatar answered Oct 11 '22 19:10

Asanka Senavirathna


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);
like image 3
Yacine Avatar answered Oct 11 '22 19:10

Yacine