I want to rotate marker as per bearing or sensor value received from Accelerometer to show the user where actually he is moving. I have set marker icon and flat value to true but its not working as required.
mCurrentLocationMarker.position(new LatLng(
LocationUtils.sLatitude, LocationUtils.sLongitude));
mCurrentLocationMarker.icon(icon);
mCurrentLocationMarker.flat(true);
mCurrentLocationMarker.rotation(LocationUtils.sBearing);
if (currentMarker != null) {
currentMarker.setPosition(new LatLng(
LocationUtils.sLatitude,
LocationUtils.sLongitude));
} else {
currentMarker = mGoogleMap
.addMarker(mCurrentLocationMarker);
}
animateCameraTo(true);
I have used this as marker.
I don't know why its not rotating as per user's direction. If anyone has any idea please kindly help me where i am making mistake.
LocationUtils.sBearing is the value of Bearing which i received from onLocationChanged or accelerometer.
Basically I want to make my marker same as google maps marker which shows user in which direction they are moving or turning.
This is an old question and it appears the API has changed since then.
I'm assuming you are able to get the devices bearing. If not here is a handy tutorial.
First thing is to create a marker we can use for bearing updates.
Note the
.flat(true)
portion. The ensures our marker is north aligned so that our bearings will work correctly even if the user rotates the map.Now when you get your bearing updates you can do the following
This assumes your marker icon has the forward direction at the top. If your marker is rotated like the one pictured, you will have to adjust the bearing to compensate before setting it to the marker. Just a simple
setRotation(bearing - 45)
should do it.Im posting this answer because people like me who are searching for a solution related to the above question might find it useful.
So here how i did it.
As @colin said you must enable
.flat(true)
to rotate markers.1.For bearing angle i have used the following code.
Here latLng1 - my old location && latLng2 - my new location
2.To rotate marker using above bearing angle i have used this code
Here
isMarkerRotating
is a boolean value. AddisMarkerRotating = false
inOnCreate
method3.using above code
In Kotlin by using Google SphericalUtil class we can get bearing by passing source and destination LatLngs like:
Then set this result 'bearing` to the marker like
Reference: https://developers.google.com/maps/documentation/android-sdk/utility/#spherical