If you use GPS for locating the user then the Location
object you get in onLocationChanged
contains the bearing.
If you only have the two coordinates (e.g. you only have coordinates from network location provider), you can use Location.bearingTo()
to calculate the bearing of two coordinates:
Location prevLoc = ... ;
Location newLoc = ... ;
float bearing = prevLoc.bearingTo(newLoc) ;
If you have a bearing, you can set the rotation of the marker using MarkerOptions.rotation()
:
mMap.addMarker(new MarkerOptions()
.position(markerLatLng)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker))
.anchor(0.5f, 0.5f)
.rotation(bearing)
.flat(true));
You have to set the anchor to the point you want to rotate around, and it's also the point you want to be at the position you set to the marker. (0.5, 0.5) is the center of the image.
atan2(dy,dx)
at which point you set the marker orientation, then rememberprevious == current
and do another loop. – Ithunnbearing
of theLocation
object? You can set it to theCameraPosition
. – Ingra