4

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?

Adolf Dsilva
  • 13,092
  • 8
  • 34
  • 45
  • 1
    What kind of animation do you try to apply? Do you want to animate movements of the marker (like a moving car animation) or just apply some animation on static marker (like blinking animation)? – Dmitry Sep 22 '15 at 17:58
  • @Dmitry i have the same question too . how to animate a route between a route instead of drawing line between them ? – ReZa Apr 28 '16 at 07:15
  • 1
    @Reza Do you want to animate marker movement from point A to point B? The simplest solution would be this: mapController.animateTo(myLocation.getGeoPoint()); Or you may want to give a try to OSMBonusPack: https://github.com/MKergall/osmbonuspack – Dmitry Apr 28 '16 at 18:52
  • @Dmitry thanks . i want to animate a car moving from departure to destination . i couldn't find any solution and osmbounspack does not have such capability. – ReZa May 01 '16 at 07:55
  • 1
    @Reza I believe you have to implement this kind of animation by youself. I can think about couple ways of doing this: (1) update location on your car on the map and use method animateTo() to keep you car in the center of the map; (2) create another layout on the top of the map and place your icon of the car in the center of this layout; then use method animateTo() to keep the map centered on location of the car. – Dmitry May 02 '16 at 19:47

1 Answers1

4

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

Community
  • 1
  • 1
zapotec
  • 2,628
  • 4
  • 31
  • 53