如何通过LatLng点的ArrayList为标记设置动画?

时间:2014-04-21 18:39:24

标签: android arraylist location google-maps-api-2 marker

我正在使用Google Maps API V2来获取当前用户的位置,并使用onLocationChanged侦听器记录他的路线。当用户记录他们的路线时,我将在每个位置检测到的所有LatLng保存在一个arraylist中。当用户停止记录他们的路线时,我在arraylist的第一个点放置一个标记。我现在的问题是我希望通过arraylist中的所有点来设置标记的动画。有人可以告诉我,我该怎么做?

需要注意的是阵列不需要排序,因为我记录了它们来的点。我尝试使用for循环遍历数组并发送值但是我从

中得到一个错误
  

final LatLng startLatLng = proj.fromScreenLocation(startPoint);

说"未知来源"以及NullPointer异常。

这是我的代码:

case R.id.action_replay:

            int o;

            for(o=0; o<oldlocPoints.size(); ++o){

                if(--o > 1){lastPos = oldlocPoints.get(--o);}
                toPos = oldlocPoints.get(++o);

                animateMarker(markerStart, lastPos, toPos);
            }
 return true;

这就是我如何尝试通过标记进行动画制作。我遇到的主要困难是在run()中它似乎只想要最终类型值,所以我不知道如何取悦它。

    //Animates marker through the locations saved from the recorded route
public void animateMarker(final Marker marker, LatLng lastPos, final LatLng toPos) {
    final long duration = 1600;
    final Handler handler = new Handler();
    this.lastPos = lastPos;
    this.toPos = toPos;
    final long start = SystemClock.uptimeMillis();
    final int o;

    Projection proj = map.getProjection();
    Point startPoint = proj.toScreenLocation(lastPos);
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);

    Log.d(TAG, "" + lastPos + "" + toPos);

    final Interpolator interpolator = new AccelerateDecelerateInterpolator();
    handler.post(new Runnable() {
        @Override
        public void run() {

            long elapsed = SystemClock.uptimeMillis() - start;
            float t = interpolator.getInterpolation((float) elapsed / duration);
            double lng = t * toPos.longitude + (1 - t) * startLatLng.longitude;
            double lat = t * toPos.latitude + (1 - t) * startLatLng.latitude;
            markerStart.setPosition(new LatLng(lat, lng));
            //markerStart.setPosition(interpolator.interpolate(t, target, replayEnd));
            if (t < 1.0) {
                //Post again 16ms later == 60 frames per second
                handler.postDelayed(this, 32);
            } else {
                //Animation ended
            }
        }
    });
}

有人可以帮帮我吗?

更新 我最近的尝试是:

while (i<oldlocPoints.size()){


                final long duration = 32;
                final Handler handler = new Handler();
                final long start = SystemClock.uptimeMillis();
                Projection proj = map.getProjection();
                final LatLng toPos = oldlocPoints.get(i/3);

                Point startPoint = proj.toScreenLocation(oldlocPoints.get(i));
                final LatLng startLatLng = proj.fromScreenLocation(startPoint);

                final Interpolator interpolator = new AccelerateDecelerateInterpolator();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        long elapsed = SystemClock.uptimeMillis() - start;
                        float t = interpolator.getInterpolation((float) elapsed / duration);
                        double lng = t * toPos.longitude + (1 - t) * startLatLng.longitude;
                        double lat = t * toPos.latitude + (1 - t) * startLatLng.latitude;
                        markerStart.setPosition(new LatLng(lat, lng));
                        //markerStart.setPosition(interpolator.interpolate(t, target, replayEnd));
                        if (t < 1.0) {
                            //Post again 16ms later == 60 frames per second
                            handler.postDelayed(this, 32);
                        } else {
                            //Animation ended
                        }
                    }
                });

                i++;
            }

1 个答案:

答案 0 :(得分:7)

试试这段代码:: 首先是MarkerAnimation类:

Select iif( m.amount>0, m.amount, "NOT PAID")
From   monthly m
Where  m.year = s.year 
And    m.month='February'

以及Google开发者为您预先编写的_latLngInterpolator:

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();
        } 

并在地图活动中将其命名为:

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)));
        }
    }
}