标记动画会创建重复项

时间:2016-02-10 10:39:10

标签: android animation google-maps-markers

我开发了一个位置感知应用并使用自定义标记作为MyLocation('blue-dot'),但只要标记移动到新位置,它就会在地图上的旧位置留下副本。

这里的代码:

标记

myMarker = mMap.addMarker(new MarkerOptions()
                .flat(true)
                .position(new LatLng(0, 0))
                .anchor(0.5f, 0.5f)
                .visible(true)
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.position)));

呼叫

if (calcDistance(mCurrentLocation, reference) > 0.1) //prevent wobbly marker
{
   animateMarker(myMarker, mCurrentLocation);
}

方法

static void animateMarker(final Marker marker, final LatLng finalPosition) {
            TypeEvaluator<LatLng> typeEvaluator = new TypeEvaluator<LatLng>() {
                @Override
                public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
                    double lat = (finalPosition.latitude - marker.getPosition().latitude) * fraction + marker.getPosition().latitude;
                    double lng = (finalPosition.longitude - marker.getPosition().longitude) * fraction + marker.getPosition().longitude;
                    return new LatLng(lat, lng);
                }
            };
            Property<Marker, LatLng> property = Property.of(Marker.class, LatLng.class, "position");
            ObjectAnimator animator = ObjectAnimator.ofObject(marker, property, typeEvaluator, finalPosition);
            animator.setDuration(3000);
            animator.start();
        }

您可能会注意到animateMarker-Method来自GoogleDevBytes视频,所以我想这不应该是问题

我做错了吗?这是谷歌Api的一个错误吗?

2 个答案:

答案 0 :(得分:0)

检查这个要点:https://gist.github.com/broady/6314689 尝试以那种方式实现

答案 1 :(得分:0)

以下更改:

if (calcDistance(mCurrentLocation, reference) > 0.1) //prevent wobbly marker
{
   animateMarker(myMarker, mCurrentLocation);
}

对此:

if (calcDistance(mCurrentLocation, reference) > 0.1) //prevent wobbly marker
{
   if (myMarker != null) {
       myMarker.remove(); 
   }
   animateMarker(myMarker, mCurrentLocation);
}
相关问题