Animating markers on OpenStreet Maps using osmdroi

2019-03-22 05:35发布

问题:

I'm using the google Maps marker animation logic given here.

My marker gets animated,but after each marker.setPosition(newPosition); I need to call mapView.invalidate();which refreshes the map resulting in very sluggish animation.

Is there any workaround?

回答1:

The next solution is working correctly for me:

import org.osmdroid.api.IGeoPoint;
import org.osmdroid.api.IMapController;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.Projection;
import org.osmdroid.views.overlay.Marker;

public void animateMarker(final Marker marker, final GeoPoint toPosition) {
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    Projection proj = map.getProjection();
    Point startPoint = proj.toPixels(marker.getPosition(), null);
    final IGeoPoint startGeoPoint = proj.fromPixels(startPoint.x, startPoint.y);
    final long duration = 500;
    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.getLongitude() + (1 - t) * startGeoPoint.getLongitude();
            double lat = t * toPosition.getLatitude() + (1 - t) * startGeoPoint.getLatitude();
            marker.setPosition(new GeoPoint(lat, lng));
            if (t < 1.0) {
                handler.postDelayed(this, 15);
            }
            map.postInvalidate();
        }
    });
}

It is based on the same implementation done by some people for GoogleMaps v2, but adapted to osmdroid.

The source where I found the implementation for GoogleMaps v2 is here: How to animate marker in android map api V2?

I am using: osmdroid-android 5.5 and osmbonuspack 6.0