Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating markers on OpenStreet Maps using osmdroid

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?

like image 681
Audi Avatar asked Oct 30 '25 13:10

Audi


1 Answers

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

like image 52
zapotec Avatar answered Nov 02 '25 05:11

zapotec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!