Sagar Nayak
Sagar Nayak

Reputation: 2218

rotate google maps with respect to the direction of movement of device (android)

i am working on app which use google maps to plot the route in which the user is moving and its showing live. so i want to rotate the map according to the direction of movement of user.

if he/she moves right then the map rotate in such a way that the route he is moving always shows in foreword direction. like google maps navigation.

i think i should take the bearing between two location and then calculate some bearing and apply it to the maps.

any suggestion will be appreciated .

thank you

update

let me clarify my question as it is not that much understandable (thats what i think).

i want the my location pointer to point forward or upward always and the map to rotate itself when i move in an direction.

i think this makes my question more clear.

Upvotes: 5

Views: 4561

Answers (2)

Harpreet
Harpreet

Reputation: 3070

import android.location.Location;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;

....

protected void animateToLocation(Location position)
    {
        if (mGoogleMap == null || position == null)
            return;

        float zoomLevel = mGoogleMap.getCameraPosition().zoom != mGoogleMap.getMinZoomLevel() ? mGoogleMap.getCameraPosition().zoom : 18.0F;
        LatLng latLng = new LatLng(position.getLatitude(), position.getLongitude());
        CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(latLng)
            .bearing(position.getBearing())
            .zoom(zoomLevel)
            .build();

        mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), null);
    }

while position:Location can be fetched from FusedApi#onLocationResult or LocationManager#OnLocationChanged

Upvotes: 0

suhas_sm
suhas_sm

Reputation: 2114

Calculate bearing between two points and then use it to set the camera position and then animate camera.

CameraPosition cameraPosition = new CameraPosition.Builder()
                                    .target(targetLatLng)
                                    .bearing(targetBearing)
                                    .zoom(mMap.getCameraPosition().zoom)
                                    .build();

mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition),
                            2000,
                            mCancelableCallback);

Upvotes: 2

Related Questions