Im發佈了此答案,因爲像我這樣的人正在尋找與上述問題相關的解決方案可能會發現它很有用。
所以在這裏,我是如何做到的。
由於@colin表示您必須啓用.flat(true)
來旋轉標記。
1.對於方位角我使用了下面的代碼。
這裏latLng1 - 我的舊位置& & latLng2 - 我的新位置
private double bearingBetweenLocations(LatLng latLng1,LatLng latLng2) {
double PI = 3.14159;
double lat1 = latLng1.latitude * PI/180;
double long1 = latLng1.longitude * PI/180;
double lat2 = latLng2.latitude * PI/180;
double long2 = latLng2.longitude * PI/180;
double dLon = (long2 - long1);
double y = Math.sin(dLon) * Math.cos(lat2);
double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
* Math.cos(lat2) * Math.cos(dLon);
double brng = Math.atan2(y, x);
brng = Math.toDegrees(brng);
brng = (brng + 360) % 360;
return brng;
}
2.To使用上述方位角我已經使用這個代碼
這裏isMarkerRotating
是一個布爾值旋轉標記。在OnCreate
方法
private void rotateMarker(final Marker marker, final float toRotation) {
if(!isMarkerRotating) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final float startRotation = marker.getRotation();
final long duration = 2000;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
isMarkerRotating = true;
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed/duration);
float rot = t * toRotation + (1 - t) * startRotation;
float bearing = -rot > 180 ? rot/2 : rot;
marker.setRotation(bearing);
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
isMarkerRotating = false;
}
}
});
}
}
3添加isMarkerRotating = false
。使用上述代碼
LatLng oldLocation, newLocaation;
float bearing = (float) bearingBetweenLocations(oldLocation, newLocaation);
rotateMarker(start_marker, bearing);
噓聲!!!任何回答這個或https://stackoverflow.com/questions/33687236/rotate-marker-as-per-user-direction-on-google-maps-v2-android ????? – Stella