1

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 litle bit confused.

Thanks

pdcc
  • 363
  • 5
  • 17
  • You want to look into orientation sensors... and probably their limitations. – zgc7009 Oct 10 '14 at 15:57
  • Possible Duplicate [this](http://stackoverflow.com/questions/14767282/android-maps-v2-rotate-mapview-with-compass) and [this](http://stackoverflow.com/questions/1830391/rotate-mapview-in-android) – Antrromet Oct 10 '14 at 16:01

1 Answers1

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

public static double degToRad(double deg){
    return deg * Math.PI / 180.0;
}
public static double radToDeg(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)));
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
  • Your functions is like this: 'float targetBearing = currentLocation.bearingTo(endingLocation)'? – pdcc Oct 13 '14 at 10:08