繁体   English   中英

Google Maps v2 上的动画标记

[英]Animating markers on Google Maps v2

使用 v2 API 在 Google 地图上为标记设置动画的最佳方法是什么?

我正在开发一个以地图为中心的游戏,我可以跟踪人们的位置并将他们显示在地图上供彼此查看。 当人们移动时,我想将一个标记从他当前的位置动画到他的最新位置。 每个人都有一个方向,所以我需要适当地旋转标记。

使用新的 Google Maps API 的最佳方法是什么?

一些谷歌工程师提供了一个很好的演示视频,其中包含一些优雅的示例代码,关于如何为所有不同版本的 Android 设置从起点到终点的动画标记:

相关代码在这里:

https://gist.github.com/broady/6314689

和一个很好的演示视频,所有这些都在行动。

http://youtu.be/WKfZsCKSXVQ

下面的旧已弃用答案

在文档中,提到不能更改标记图标:

图标

为标记显示的位图。 如果图标未设置,则显示默认图标。 您可以使用 defaultMarker(float) 指定默认图标的替代颜色。 创建标记后,您将无法更改图标。

Google Maps API v2 文档

您将不得不跟踪特定标记,可能使用类似于此处描述的方法: 将标记链接到对象,然后找出需要更新的标记。 在标记上调用.remove() ,然后根据您想要的“方向”创建一个旋转的图像,用该图像创建一个新的标记,并将新的标记添加到地图中。

您不需要“清除”地图,只需删除要修改的标记,创建一个新标记,然后将其添加回地图即可。

不幸的是,新的 Maps API 还不是很灵活。 希望谷歌继续改进它。

DiscDev 答案的完整示例(上):

LatLng fromLocation = new LatLng(38.5, -100.4); // Whatever origin coordinates
LatLng toLocation = new LatLng(37.7, -107.7); // Whatever destination coordinates
Marker marker = mMap.addMarker(new MarkerOptions().position(firstLocation));
MarkerAnimation.animateMarkerToICS(marker, toLocation, new LatLngInterpolator.Spherical());

对于那些使用 GPS/或任何接收位置更新的位置提供程序的人:

Marker ourGlobalMarker;
// We've got a location from some provider of ours, now we can call:
private void updateMarkerPosition(Location newLocation) {

    LatLng newLatLng = new LatLng(newLocation.getLatitude(), newLocation.getLongitude());
    
    if(ourGlobalMarker == null) { // First time adding marker to map
        ourGlobalMarker = mMap.addMarker(new MarkerOptions().position(newLatLng));
    }
    else {
        MarkerAnimation.animateMarkerToICS(ourGlobalMarker, newLatLng, new LatLngInterpolator.Spherical());
    }         
}

重要的:

1MarkerAnimation.java如果动画持续时间设置为 X,并且您以小于 X 的速率接收位置更新,则会触发多个动画,并且您可能会看到标记动画有点闪烁(这不是很好的用户体验) )。

为了避免这种情况, animationMarkerToICS方法(例如我在这里使用animationMarkerToICS )应该看起来像这样,

完整的方法实现:

private static Animator animator; // MAKING ANIMATOR GLOBAL INSTEAD OF LOCAL TO THE STATIC FUNCTION

...

// Ice Cream Sandwich compatible
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static void animateMarkerToICS(Marker marker, LatLng finalPosition, final LatLngInterpolator latLngInterpolator) {

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

    // ADD THIS TO STOP ANIMATION IF ALREADY ANIMATING TO AN OBSOLETE LOCATION
    if(animator != null && animator.isRunning()) {
        animator.cancel();
        animator = null;
    }
    animator = ObjectAnimator.ofObject(marker, property, typeEvaluator, finalPosition);
    animator.setDuration((long) ANIMATION_DURATION);
    animator.start();
}

享受。

自 API v2 的 rev.7 起,Marker 添加了一个新功能。 Marker.setIcon ,因此您可以使用多个图标来显示方向。

    //Your code         
    double bearing = 0.0;
             bearing = getBearing(new LatLng(
                                                currentPosition.latitude
                                                ,currentPosition.longitude),
                                        new LatLng(
                                                nextPosition.latitude,
                                                nextPosition.longitude));  

          bearing -= 90;
                            CameraPosition cameraPosition = new CameraPosition
                                    .Builder()
                                    .target(new LatLng(nextPosition.latitude, nextPosition.longitude))
                                    .bearing((float) bearing)
                                    .zoom(ZOOM_LEVEL).build();


                            mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 5000, null);

                 animatedMarker(currentPosition,nextPosition,busMarker);



                //Method for finding bearing between two points
                    private float getBearing(LatLng begin, LatLng end) {
                        double lat = Math.abs(begin.latitude - end.latitude);
                        double lng = Math.abs(begin.longitude - end.longitude);
                        if (begin.latitude < end.latitude && begin.longitude < end.longitude)
                            return (float) (Math.toDegrees(Math.atan(lng / lat)));
                        else if (begin.latitude >= end.latitude && begin.longitude < end.longitude)
                            return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 90);
                        else if (begin.latitude >= end.latitude && begin.longitude >= end.longitude)
                            return (float) (Math.toDegrees(Math.atan(lng / lat)) + 180);
                        else if (begin.latitude < end.latitude && begin.longitude >= end.longitude)
                            return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 270);
                        return -1;
                    }

   private void animatedMarker(final LatLng startPosition,final LatLng nextPosition,final Marker mMarker)
    {

        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        final Interpolator interpolator = new AccelerateDecelerateInterpolator();
        final float durationInMs = 3000;
        final boolean hideMarker = false;

        handler.post(new Runnable() {
            long elapsed;
            float t;
            float v;

            @Override
            public void run() {
                // Calculate progress using interpolator
                elapsed = SystemClock.uptimeMillis() - start;
                t = elapsed / durationInMs;
                v = interpolator.getInterpolation(t);

                LatLng currentPosition = new LatLng(
                        startPosition.latitude * (1 - t) + nextPosition.latitude * t,
                        startPosition.longitude * (1 - t) + nextPosition.longitude * t);

                mMarker.setPosition(currentPosition);

                // Repeat till progress is complete.
                if (t < 1) {
                    // Post again 16ms later.
                    handler.postDelayed(this, 16);
                } else {
                    if (hideMarker) {
                        mMarker.setVisible(false);
                    } else {
                        mMarker.setVisible(true);
                    }
                }
            }
        });

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM