14

I am using the Google Maps Android API v2 where I display my Device location as a GoogleMap Marker on a Map. I have a listener (LocationServices.FusedLocationApi.requestLocationUpdates...) attach to the Map in order to handle every GPS location change. Every time I receive a new location I update the Marker (my position) as well as the Camera on the Map and pretty much it shows like I am moving as I drive.

However... I need some help in order to have the MAP rotate and/or display on the same direction that I am moving to. I mean, while I drive I need to rotate the MAP where I can see on my map-left-side what I have on my real-life-left-side, and so the same with the right. Since the MAP direction is static, the Marker (Me) is moving on it but most of the time the direction does not match my car real-time direction.

What should I do with the Map in order to accomplish this visual effect?

G305
  • 895
  • 2
  • 9
  • 13
  • 1
    So technically, you want to implement the sensorlisteners, then have the map react to it based on the data in the callback (if left or right). Do check this posts: http://stackoverflow.com/questions/6224577/, http://stackoverflow.com/q/2317428/4625829, http://stackoverflow.com/q/14320015/4625829, http://stackoverflow.com/q/4308262/4625829, http://stackoverflow.com/q/36258490/4625829, http://stackoverflow.com/q/18162642/4625829 – AL. May 26 '16 at 03:42

1 Answers1

29

I was guessing how is posible that the Google Map API does not provide such common visual effect desire. I was wrong... it's called BEARING.

private void updateCameraBearing(GoogleMap googleMap, float bearing) {
    if ( googleMap == null) return;
    CameraPosition camPos = CameraPosition
            .builder(
                    googleMap.getCameraPosition() // current Camera
            )
            .bearing(bearing)
            .build();                   
    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos)); 
}

Since I setup my location changes to be notified every 2 seconds...

    mLocationRequest = LocationRequest.create()
                        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                        .setInterval(POLLING_FREQ_2SECONDS)
                        .setFastestInterval(FASTEST_UPDATE_FREQ_2_SECONDS)

I use that same place to process the new location as well as to update the Camera (new BEARING):

public void onLocationChanged(Location location) {

 // ... process location

 updateCameraBearing(googleMap, location.getBearing());
}

TESTED + WORKING = DONE!

G305
  • 895
  • 2
  • 9
  • 13
  • 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. https://stackoverflow.com/questions/48123738/gmap-bearing-rotation-in-smooth-motion – Hannan Jan 08 '18 at 09:41
  • in V2 and V3 you can use @G305 answer in the onCameraIdle() method after implementing GoogleMap.OnCameraIdleListener in your Map activity or fragment – Mofor Emmanuel Mar 30 '21 at 09:02