如何在android中的两个位置之间平滑地移动谷歌地图标记

时间:2017-10-08 08:40:44

标签: android google-maps google-maps-api-3

我需要像example一样顺利移动谷歌地图标记。在我的情况下还有一些开始和结束位置的航点。我已经使用下面的代码了。但它在任何时候都没有恒定的速度。

public static void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint, final Bitmap bitmap) {


    Marker marker = myMap.addMarker(new MarkerOptions()
            .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
            .position(directionPoint.get(0))
            .flat(true));

    Marker marker1 = myMap.addMarker(new MarkerOptions()
            .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
            .position(directionPoint.get(10))
            .flat(true));

    myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 10));

    animateMarker(myMap, marker, marker1, directionPoint, false);
}


private static void animateMarker(GoogleMap myMap, final Marker marker, final Marker marker1, final List<LatLng> directionPoint,
                                  final boolean hideMarker) {
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    Projection proj = myMap.getProjection();
    final long duration = (long) (30000);

    final Interpolator interpolator = new LinearInterpolator();

    handler.post(new Runnable() {
        int i = 0;

        @Override
        public void run() {
            long elapsed = SystemClock.uptimeMillis() - start;
            float t = interpolator.getInterpolation((float) elapsed
                    / duration);
            if (i < directionPoint.size()){
                marker.setPosition(directionPoint.get(i));
                marker1.setPosition(directionPoint.get(i+10));
            }
            i++;


            if (t < 1.0) {
                // Post again 16ms later.
                handler.postDelayed(this, 16);
            } else {
                if (hideMarker) {
                    marker.setVisible(false);
                } else {
                    marker.setVisible(true);
                }
            }
        }
    });
}

另外我需要根据两个位置之间的路线来转动标记。

0 个答案:

没有答案