4
我遵循這個答案,它的工作很棒。 https://stackoverflow.com/a/37048987/4209417Google Maps API v2中的旋轉標記
但我現在面臨的問題是:
- 當我停止在任何位置,其並不穩定。即使在我沒有移動的情況下,我也會獲得隨機軸承的價值
- 當我輪流逆時針旋轉時,這是錯誤的。它應該短暫輪流。
這是我使用的代碼:
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;
}
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);
CameraPosition camPos = CameraPosition
.builder(mMap.getCameraPosition())
.bearing(bearing)
.target(marker.getPosition())
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos));
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
isMarkerRotating = false;
}
}
});
}
}
在onLocationChanged()
float toRotation = (float) bearingBetweenLocations(toLatLng(oldLocation), toLatLng(newLocation));
rotateMarker(my_marker, toRotation);
嘿,對不起,重新審視這個老問題,但你介意發佈你的LatLngInterpolator代碼? –
@VasiliFedotov這裏是鏈接https://gist.github.com/hardikbhalodi/671ab28c81d3392fba45bc72fa549be2 LatLngInterpolator – satti8893