Search code examples
androidrotationandroid-mapviewforward

How to rotate mapview to moving direction


I have an android application with google map. I want to rotate the map view after getting the route direction to forward view.. How can I do it? Picture 1- Backward view ![This is the backward direction view. I need to rotate it to forward][1]

https://i.sstatic.net/gKkGe.png


Solution

  • You can get bearing from the location, that you received from the LocationManager and set it to the CameraPosition, like this:

        Location location = googleMap.getMyLocation();
        if (location != null) {
            CameraPosition position = CameraPosition.builder()
                    .bearing(location.getBearing())
                    .target(new LatLng(location.getLatitude(), location.getLongitude()))
                    .zoom(googleMap.getCameraPosition().zoom)
                    .tilt(googleMap.getCameraPosition().tilt)
                    .build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(position));
        }
    

    Then your camera will rotate to your direction;