Как анимировать маркер в Android Map API V2?

Я хочу реализовать плавный переход для имитации движения автомобильного маркера по карте.

Можно ли анимировать маркер в Android Map API v2?


person Alexey Zakharov    schedule 14.12.2012    source источник
comment
Как я понимаю, вам нужно нативное поведение карт API V2, чтобы плавно перемещать маркер из одной позиции в другую?   -  person Paul Annekov    schedule 13.01.2013
comment
вы можете просмотреть этот пост stackoverflow .com/questions/13728041/   -  person Pavel Dudka    schedule 03.04.2013
comment
возможный дубликат Как анимировать маркер, когда добавляется в карту на Android?   -  person bummi    schedule 02.05.2015


Ответы (3)


Ни одна из предоставленных версий мне не подошла, поэтому я реализовал собственное решение. Он обеспечивает как анимацию местоположения, так и вращение.

/**
 * Method to animate marker to destination location
 * @param destination destination location (must contain bearing attribute, to ensure
 *                    marker rotation will work correctly)
 * @param marker marker to be animated
 */
public static void animateMarker(Location destination, Marker marker) {
    if (marker != null) {
        LatLng startPosition = marker.getPosition();
        LatLng endPosition = new LatLng(destination.getLatitude(), destination.getLongitude());

        float startRotation = marker.getRotation();

        LatLngInterpolator latLngInterpolator = new LatLngInterpolator.LinearFixed();
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.setDuration(1000); // duration 1 second
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override public void onAnimationUpdate(ValueAnimator animation) {
                try {
                    float v = animation.getAnimatedFraction();
                    LatLng newPosition = latLngInterpolator.interpolate(v, startPosition, endPosition);
                    marker.setPosition(newPosition);
                    marker.setRotation(computeRotation(v, startRotation, destination.getBearing()));
                } catch (Exception ex) {
                    // I don't care atm..
                }
            }
        });

        valueAnimator.start();
    }
}

Вычисление вращения для указанной части анимации. Маркер вращается в направлении, которое ближе от начала вращения к концу:

private static float computeRotation(float fraction, float start, float end) {
    float normalizeEnd = end - start; // rotate start to 0
    float normalizedEndAbs = (normalizeEnd + 360) % 360;

    float direction = (normalizedEndAbs > 180) ? -1 : 1; // -1 = anticlockwise, 1 = clockwise
    float rotation;
    if (direction > 0) {
        rotation = normalizedEndAbs;
    } else {
        rotation = normalizedEndAbs - 360;
    }

    float result = fraction * rotation + start;
    return (result + 360) % 360;
} 

И, наконец, Google LatLngInterpolator:

private interface LatLngInterpolator {
    LatLng interpolate(float fraction, LatLng a, LatLng b);

    class LinearFixed implements LatLngInterpolator {
        @Override
        public LatLng interpolate(float fraction, LatLng a, LatLng b) {
            double lat = (b.latitude - a.latitude) * fraction + a.latitude;
            double lngDelta = b.longitude - a.longitude;
            // Take the shortest path across the 180th meridian.
            if (Math.abs(lngDelta) > 180) {
                lngDelta -= Math.signum(lngDelta) * 360;
            }
            double lng = lngDelta * fraction + a.longitude;
            return new LatLng(lat, lng);
        }
    }
}
person skywall    schedule 16.04.2016
comment
Я пытаюсь реализовать это, как мне добавить подшипник к месту назначения? У вас есть какой-либо источник на github окончательного проекта? - person Frank Odoom; 03.08.2017
comment
@FrankOdoom Вы должны правильно установить азимут с помощью метода setBearing(float) или bearingTo(Location) для переданного параметра Location destination. - person skywall; 04.08.2017
comment
Это лучший ответ, который я нашел. Мерцания нет вообще. - person Vihanga Yasith; 11.12.2017
comment
как установить подшипник - person M.Yogeshwaran; 04.10.2018
comment
@M.Yogeshwaran посмотрите два сообщения в этом обсуждении. - person skywall; 12.10.2018
comment
Лучший ответ, палец вверх - person phourxx; 21.01.2019

Попробуйте приведенный ниже код, чтобы анимировать маркер на Google Map V2. Вам нужно использовать класс Interpolator, чтобы применить анимацию к маркеру и обработать ее в обработчике анимации, как показано ниже:

   public void animateMarker(final Marker marker, final LatLng toPosition,
        final boolean hideMarker) {
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    Projection proj = mGoogleMapObject.getProjection();
    Point startPoint = proj.toScreenLocation(marker.getPosition());
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);
    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.longitude + (1 - t)
                    * startLatLng.longitude;
            double lat = t * toPosition.latitude + (1 - t)
                    * startLatLng.latitude;
            marker.setPosition(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);
                }
            }
        }
    });
}
person GrIsHu    schedule 11.04.2013
comment
Большое спасибо. Это было чрезвычайно полезно. - person Anil; 23.04.2013
comment
Как именно вы реализуете этот код? @Анил? можно ли заставить маркер двигаться по координатам gps? Пожалуйста, опубликуйте ответ. - person momokjaaaaa; 18.06.2013
comment
Эй, большое спасибо. Оно работает. Но маркер перемещается вперед и назад до запуска анимации маркера. Не могли бы вы рассказать мне, как это исправить. - person Sadia; 30.10.2013
comment
@GrlsHu Я получил код, похожий на ваш, но я не могу анимировать реальную дорогу, она анимируется по прямой линии, можете ли вы сказать мне, как этого добиться? - person Illegal Argument; 20.11.2013
comment
@IllegalArgument Ознакомьтесь с ddewaele.github.io/GoogleMapsV2WithActionBarSherlock/part3, который поможет вам. - person GrIsHu; 20.11.2013
comment
@GrIsHu Я сделал так, что проблема в том, что иногда маркеры размещаются даже над зданиями, а не на пути, поэтому я попытался сделать это с помощью полилинии. - person Illegal Argument; 20.11.2013
comment
Должен ли я not вызывать builder.include(vehicleLocation); вместо ? - person Siddharth; 22.08.2016

Только что реализовал версию, попробуйте это

public class MarkerAnimation {
static GoogleMap map;
ArrayList<LatLng> _trips = new ArrayList<>() ;
Marker _marker;
LatLngInterpolator _latLngInterpolator = new LatLngInterpolator.Spherical();

public void animateLine(ArrayList<LatLng> Trips,GoogleMap map,Marker  marker,Context current){
    _trips.addAll(Trips);
    _marker = marker;

animateMarker();
}

    public void animateMarker() {
        TypeEvaluator<LatLng> typeEvaluator = new TypeEvaluator<LatLng>() {
            @Override
            public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
                return _latLngInterpolator.interpolate(fraction, startValue, endValue);
            }
        };
        Property<Marker, LatLng> property = Property.of(Marker.class, LatLng.class, "position");

        ObjectAnimator animator = ObjectAnimator.ofObject(_marker, property, typeEvaluator, _trips.get(0));

        //ObjectAnimator animator = ObjectAnimator.o(view, "alpha", 0.0f);
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationCancel(Animator animation) {
                //  animDrawable.stop();
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                //  animDrawable.stop();
            }

            @Override
            public void onAnimationStart(Animator animation) {
                //  animDrawable.stop();
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                //  animDrawable.stop();
                if (_trips.size() > 1) {
                    _trips.remove(0);
                    animateMarker();
                }
            }
        });

        animator.setDuration(300);
        animator.start();
    } 

Класс LatLngInterpolator предварительно написан ребятами из Google, и вы можете использовать его следующим образом:

public interface LatLngInterpolator {

public LatLng interpolate(float fraction, LatLng a, LatLng b);

public class Spherical implements LatLngInterpolator {
    @Override
    public LatLng interpolate(float fraction, LatLng from, LatLng to) {
        // http://en.wikipedia.org/wiki/Slerp
        double fromLat = toRadians(from.latitude);
        double fromLng = toRadians(from.longitude);
        double toLat = toRadians(to.latitude);
        double toLng = toRadians(to.longitude);
        double cosFromLat = cos(fromLat);
        double cosToLat = cos(toLat);

        // Computes Spherical interpolation coefficients.
        double angle = computeAngleBetween(fromLat, fromLng, toLat, toLng);
        double sinAngle = sin(angle);
        if (sinAngle < 1E-6) {
            return from;
        }
        double a = sin((1 - fraction) * angle) / sinAngle;
        double b = sin(fraction * angle) / sinAngle;

        // Converts from polar to vector and interpolate.
        double x = a * cosFromLat * cos(fromLng) + b * cosToLat * cos(toLng);
        double y = a * cosFromLat * sin(fromLng) + b * cosToLat * sin(toLng);
        double z = a * sin(fromLat) + b * sin(toLat);

        // Converts interpolated vector back to polar.
        double lat = atan2(z, sqrt(x * x + y * y));
        double lng = atan2(y, x);
        return new LatLng(toDegrees(lat), toDegrees(lng));
    }

    private double computeAngleBetween(double fromLat, double fromLng, double toLat, double toLng) {
        // Haversine's formula
        double dLat = fromLat - toLat;
        double dLng = fromLng - toLng;
        return 2 * asin(sqrt(pow(sin(dLat / 2), 2) +
                cos(fromLat) * cos(toLat) * pow(sin(dLng / 2), 2)));
    }
}
}

Затем создайте экземпляр объекта класса MarkerAnimation и вызовите метод следующим образом:

 MarkerAnimation.animateLine(TripPoints,map,MovingMarker,context); 
person Chinmoy Panda    schedule 09.10.2015