Cramoss
Cramoss

Reputation: 11

How can I rotate a marker towards another point (Location)?

I basically want to know how it'd be possible to calculate and rotate a marker towards another Location point (Lat & Lng).

This is using the Google Maps API v2 for Android.

Upvotes: 1

Views: 166

Answers (1)

danny117
danny117

Reputation: 5651

Use a custom bitmap for your marker and rotate it with a matrix before creating the bitmapdescriptor and displaying the marker.

works with values of 90, 270 but something similar should work for you.

Matrix matrix = new Matrix();
matrix.postRotate(orientation); // anti-clockwise
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0,
bm.getWidth(),  bm.getHeight(), matrix, true);
bm = rotatedBitmap.copy(rotatedBitmap.getConfig(), true);

BitmapDescriptor bd = BitmapDescriptorFactory.fromBitmap(bm);

markeroptions.icon(bd);

This is from the google map utils on github

https://github.com/googlemaps/android-maps-utils

/**
 * Returns the heading from one LatLng to another LatLng. Headings are
 * expressed in degrees clockwise from North within the range [-180,180).
 * @return The heading in degrees clockwise from north.
 */
public static double computeHeading(LatLng from, LatLng to) {
    // http://williams.best.vwh.net/avform.htm#Crs
    double fromLat = toRadians(from.latitude);
    double fromLng = toRadians(from.longitude);
    double toLat = toRadians(to.latitude);
    double toLng = toRadians(to.longitude);
    double dLng = toLng - fromLng;
    double heading = atan2(
            sin(dLng) * cos(toLat),
            cos(fromLat) * sin(toLat) - sin(fromLat) * cos(toLat) * cos(dLng));
    return wrap(toDegrees(heading), -180, 180);
}

Upvotes: 1

Related Questions