在Google Maps Android中无法顺利跟踪折线

时间:2018-07-21 07:23:14

标签: android google-maps google-maps-markers google-polyline

我正在使用以下代码为Google Maps上的折线跟踪设置动画。根据LatLong列表(markerPojoList)跟踪折线。 我在某种程度上已经实现了平滑的跟踪,但是我希望这些线能够以更加平滑的方式进行跟踪。

`private void animateMarkers(final Marker marker) {
  final PolylineOptions polylineOptions = new PolylineOptions().color(Color.RED);
  final Handler handler = new Handler();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 
     1);
                    valueAnimator.setDuration(0);
                    valueAnimator.setInterpolator(new LinearInterpolator());
                    valueAnimator.addUpdateListener(new 
                    ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator 
         valueAnimator) {     
                        v = valueAnimator.getAnimatedFraction();                  
                        if (i < markerPojoList.size()){         
                        lng = v * markerPojoList.get((i + 1) - 
         1).getLongitude() + 
                        (1 - v)   * markerPojoList.get(i).getLongitude();
                        lat = v * markerPojoList.get((i + 1) - 1).getLattitude() + 
                        (1 - v)markerPojoList.get(i).getLattitude();
                        LatLng newPos = new LatLng(lat, lng);
                        marker.setPosition(newPos);
                        polylineOptions.add(newPos);
                        line = mMap.addPolyline(polylineOptions);
                        mMap.moveCamera(CameraUpdateFactory
                        .newCameraPosition(new CameraPosition.Builder()
                        .target(newPos)
                        .zoom(mMap.getCameraPosition().zoom)
                        .build()));
}}});
valueAnimator.start();
i++;
if (i < markerPojoList.size())
handler.postDelayed(this, 10);
}
});
}`

1 个答案:

答案 0 :(得分:0)

我已经编辑了您的代码,请尝试

private void animateMarkers(final Marker marker) {
    final PolylineOptions polylineOptions = new PolylineOptions().color(Color.RED);
    final Handler handler = new Handler();
    handler.post(new Runnable() {
        @Override
        public void run() {
            ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,
                    1);
            valueAnimator.setDuration(0);
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator
                                                      valueAnimator) {
                    if (i < markerPojoList.size()) {
                        lng = v * markerPojoList.get((i + 1) - 1).getLongitude() + markerPojoList.get(i).getLongitude();
                        lat = v * markerPojoList.get((i + 1) - 1).getLattitude() + markerPojoList.get(i).getLattitude();
                        LatLng newPos = new LatLng(lat, lng);
                        marker.setPosition(newPos);
                        polylineOptions.add(newPos);
                        line = mMap.addPolyline(polylineOptions);
                        mMap.moveCamera(CameraUpdateFactory
                                .newCameraPosition(new CameraPosition.Builder()
                                        .target(newPos)
                                        .zoom(mMap.getCameraPosition().zoom)
                                        .build()));
                    }
                }
            });
            valueAnimator.start();
            i++;
            if (i < markerPojoList.size())
                handler.postDelayed(this, 10);
        }
    });
}