我必须在地图上绘制标记和路径;我想将标记移动到路径上,同时旋转它。我的标记正在移动,但不是沿着路径移动。这是我的代码:
标记动画方法
public void animateMarker(final int position, final LatLng startPosition, final LatLng toPosition,
final boolean hideMarker) {
Location source = new Location(LocationManager.GPS_PROVIDER);
source.setLatitude(sourceLat);
source.setLongitude(sourceLon);
Location dest = new Location(LocationManager.GPS_PROVIDER);
dest.setLatitude(destLat);
dest.setLongitude(destLon);
final Marker marker = mMap.addMarker(new MarkerOptions()
.position(startPosition)
.title("My Car")
.anchor(0.5f,0.5f)
.snippet("Gurugram")
.rotation(new Location(source).bearingTo(dest))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_car)));
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final long duration = 50000;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
double lng = t * toPosition.longitude + (1 - t)
* startPosition.longitude;
double lat = t * toPosition.latitude + (1 - t)
* startPosition.latitude;
marker.setPosition(new LatLng(lat, lng));
if(preLatLng==null){
preLatLng=new LatLng(lat,lng);
}
else{
float bearing = (float) bearingBetweenLocations(preLatLng,new LatLng(lat,lng));
rotateMarker(marker, bearing);
marker.setZIndex(bearing);
preLatLng=new LatLng(lat, lng);
}
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}
方位法
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;
}
标记旋转方法rotateMarker
使用旋转值旋转标记。
private boolean isMarkerRotating=false;
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;
}
}
});
}
}