如何使用 Google Maps SDK 中的自定义 LocationSource 平滑地为当前位置设置动画?

问题描述

我使用的是自定义 LocationSource,它每 300 多毫秒提供一次更新。

在下面您可以观察到坐标更新使蓝点(当前位置)以相当不稳定的方式移动(我移动缓慢以使效果更明显):

Current not so smooth movement

如何让蓝点的移动更流畅,类似于 Google 地图应用程序中的移动?更少的左右摇晃和更平滑的向前移动而不是从 A 点“跳跃”到 B 点会很棒。

有些答案建议为相机设置动画或使用插值器在预设时间范围内为 2 个已知位置之间的移动设置动画,但这并不真正适用于我的情况。

解决方法

Use SnapToRoad Api to get correct co-ordinates from one location to other location.  



 public void animateMarker(int startPositionId,final LatLng toPosition,Marker marker,final boolean hideMarke) {

        final long start = SystemClock.uptimeMillis();
        Projection proj = routeMap.getProjection();
        Point startPoint = proj.toScreenLocation(marker.getPosition());
        final LatLng startLatLng = proj.fromScreenLocation(startPoint);
        final long duration = 1000;

        final Interpolator interpolator = new LinearInterpolator();
        runnable = new Runnable() {
            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - start;


                float t = interpolator.getInterpolation((float) elapsed
                        / duration);

                Log.d("run:t",String.valueOf(t));

                double lng = t * toPosition.longitude + (1 - t)
                        * startLatLng.longitude;
                double lat = t * toPosition.latitude + (1 - t)
                        * startLatLng.latitude;


                Log.d("run:lat",String.valueOf(lat));
                Log.d("run:lng",String.valueOf(lng));
                marker.setPosition(new LatLng(lat,lng));
             

                if (t < 1.0) {
                    // Post again 16ms later.
                   

                    handlerSetPosition.postDelayed(this,8);


                } else {
                    if (hideMarke) {
                        marker.setVisible(false);
                    } else {
                        marker.setVisible(true);
                        pointId = startPositionId + 1;
                        getSnappedRout(pointId);


                    }
                }
            }
        };
        handlerSetPosition.post(runnable);

    
    }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...