Skip to content Skip to sidebar Skip to footer

Rotate Map According Current Walk Direction Android Google Maps

I have an android application with a map of google. Can anybody tell me how we rotate the map according user current direction walking? I have read a lot of information but still a

Solution 1:

You can calculate bearing by comparing two points. You said that your user will be walking so that shouldn't be too hard to get two points a distance apart from each other.

When you have the two points do some math like so

lon1 = degToRad(lon1);
lon2 = degToRad(lon2);
lat1 = degToRad(lat1);
lat2 = degToRad(lat2);

double a = Math.Sin(lon2 - lon1) * Math.Cos(lat2);
double b = Math.Cos(lat1) * Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(lon2 - lon1);
double c = radToDeg(Math.Atan2(a, b)); // c is our bearing //

These are our conversion functions

publicstaticdoubledegToRad(double deg){
    return deg * Math.PI / 180.0;
}
publicstaticdoubleradToDeg(double rad){
    rad = rad * (180.0 / Math.PI);
    if (rad < 0) rad = 360.0 + rad;
    return rad;
 }

Once you have the bearing you can pass it to your map using the CameraUpdateFactory.

map.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(LatLng, zoom, tilt, bearing)));

Post a Comment for "Rotate Map According Current Walk Direction Android Google Maps"