Animating markers on OpenStreet Maps using osmdroid

前提是你 提交于 2019-12-09 07:05:12

问题


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



来源:https://stackoverflow.com/questions/31337149/animating-markers-on-openstreet-maps-using-osmdroid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!