如何使用沿折线的实时位置更新标记?

How to update marker using live location along a polyline?

我的问题标题似乎是现有的,但这是我的完整场景。

我有一个 activity 用于基于地图的操作,我在其中沿着道路绘制多段线,比方说两个位置之间的路线。基本上,该应用程序会跟踪用户的当前位置(驾车旅行)。所以直到部分一切正常,如正确显示路线,设备位置 API 正在提供位置更新(有点精确),而且我能够顺利地更改位置更新,

所以问题是,位置更新有时是曲折的,有时可能不会碰到路,位置更新会到处都是。

我也研究了 ROAD api,但我没有得到正确的帮助,即使是从之前提出的一些问题中也是如此。

是否可以让标记只沿着道路移动?

我们将不胜感激。

您可以通过将标记投影到最近的路径段来将标记捕捉到路径。您可以通过 PolyUtil.isLocationOnPath():

找到最近的路段

PolyUtil.isLocationOnPath(carPos, segment, true, 30)

以及标记到该段的投影,您可以通过将测地线球坐标转换为正交屏幕坐标计算投影正交坐标并将其转换回球面坐标 (WGS84 LatLng -> Screen x,y -> WGS84 LatLng):

Point carPosOnScreen = projection.toScreenLocation(carPos);
Point p1 = projection.toScreenLocation(segment.get(0));
Point p2 = projection.toScreenLocation(segment.get(1));
Point carPosOnSegment = new Point();

float denominator = (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y);
// p1 and p2 are the same
if (Math.abs(denominator) <= 1E-10) {
    markerProjection = segment.get(0);
} else {
    float t = (carPosOnScreen.x * (p2.x - p1.x) - (p2.x - p1.x) * p1.x
            + carPosOnScreen.y * (p2.y - p1.y) - (p2.y - p1.y) * p1.y) / denominator;
    carPosOnSegment.x = (int) (p1.x + (p2.x - p1.x) * t);
    carPosOnSegment.y = (int) (p1.y + (p2.y - p1.y) * t);
    markerProjection = projection.fromScreenLocation(carPosOnSegment);
}

完整源代码:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mGoogleMap;
    private MapFragment mapFragment;

    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map_fragment);
        mapFragment.getMapAsync(this);

        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        mGoogleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                List<LatLng> sourcePoints = new ArrayList<>();
                PolylineOptions polyLineOptions;
                LatLng carPos;

                sourcePoints.add(new LatLng(-35.27801,149.12958));
                sourcePoints.add(new LatLng(-35.28032,149.12907));
                sourcePoints.add(new LatLng(-35.28099,149.12929));
                sourcePoints.add(new LatLng(-35.28144,149.12984));
                sourcePoints.add(new LatLng(-35.28194,149.13003));
                sourcePoints.add(new LatLng(-35.28282,149.12956));
                sourcePoints.add(new LatLng(-35.28302,149.12881));
                sourcePoints.add(new LatLng(-35.28473,149.12836));

                polyLineOptions = new PolylineOptions();
                polyLineOptions.addAll(sourcePoints);
                polyLineOptions.width(10);
                polyLineOptions.color(Color.BLUE);
                mGoogleMap.addPolyline(polyLineOptions);

                carPos = new LatLng(-35.281120, 149.129721);
                addMarker(carPos);
                mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sourcePoints.get(0), 15));

                for (int i = 0; i < sourcePoints.size() - 1; i++) {
                    LatLng segmentP1 = sourcePoints.get(i);
                    LatLng segmentP2 = sourcePoints.get(i+1);
                    List<LatLng> segment = new ArrayList<>(2);
                    segment.add(segmentP1);
                    segment.add(segmentP2);

                    if (PolyUtil.isLocationOnPath(carPos, segment, true, 30)) {
                        polyLineOptions = new PolylineOptions();
                        polyLineOptions.addAll(segment);
                        polyLineOptions.width(10);
                        polyLineOptions.color(Color.RED);
                        mGoogleMap.addPolyline(polyLineOptions);
                        LatLng snappedToSegment = getMarkerProjectionOnSegment(carPos, segment, mGoogleMap.getProjection());
                        addMarker(snappedToSegment);
                        break;
                    }
                }
            }
        });
        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sourcePoints.get(0), 15));
    }

    private LatLng getMarkerProjectionOnSegment(LatLng carPos, List<LatLng> segment, Projection projection) {
        LatLng markerProjection = null;

        Point carPosOnScreen = projection.toScreenLocation(carPos);
        Point p1 = projection.toScreenLocation(segment.get(0));
        Point p2 = projection.toScreenLocation(segment.get(1));
        Point carPosOnSegment = new Point();

        float denominator = (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y);
        // p1 and p2 are the same
        if (Math.abs(denominator) <= 1E-10) {
            markerProjection = segment.get(0);
        } else {
            float t = (carPosOnScreen.x * (p2.x - p1.x) - (p2.x - p1.x) * p1.x
                    + carPosOnScreen.y * (p2.y - p1.y) - (p2.y - p1.y) * p1.y) / denominator;
            carPosOnSegment.x = (int) (p1.x + (p2.x - p1.x) * t);
            carPosOnSegment.y = (int) (p1.y + (p2.y - p1.y) * t);
            markerProjection = projection.fromScreenLocation(carPosOnSegment);
        }    
        return markerProjection;
    }

    public void addMarker(LatLng latLng) {
        mGoogleMap.addMarker(new MarkerOptions()
                .position(latLng)
        );
    }
}

你会得到类似的东西:

但更好的方法是计算汽车与路径起点的距离,并通过 SphericalUtil.interpolate() 找到它在路径上的位置,因为如果几个路径段彼此靠近(例如在同一条道路的不同车道上),例如那:

到当前汽车位置可能是最近的 "wrong" 路段。因此,计算汽车与路线起点的距离,并使用 SphericalUtil.interpolate() 确定路径上的点。

我知道这个问题很老了,但为了以防万一有人需要它,添加到 Andrii Omelchenko 的回答中,这是您可以使用 SphericalUtil.interpolate() 准确找到线段上的点的一种方法:

private LatLng getMarkerProjectionOnSegment(LatLng carPos, List<LatLng> segment, Projection projection) {
        Point a = projection.toScreenLocation(segment.get(0));
        Point b = projection.toScreenLocation(segment.get(1));
        Point p = projection.toScreenLocation(carPos);

        if(a.equals(b.x, b.y)) return segment.get(0); // Projected points are the same, segment is very short
        if(p.equals(a.x, a.y) || p.equals(b.x, b.y)) return carPos;

        /*
        If you're interested in the math (d represents point on segment you are trying to find):
        
        angle between 2 vectors = inverse cos of (dotproduct of 2 vectors / product of the magnitudes of each vector)
        angle = arccos(ab.ap/|ab|*|ap|)
        ad magnitude = magnitude of vector ap multiplied by cos of (angle).
        ad = ap*cos(angle) --> basic trig adj = hyp * cos(opp)
        below implementation is just a simplification of these equations
         */

        float dotproduct = ((b.x-a.x)*(p.x-a.x)) + ((b.y-a.y)*(p.y-a.y));
        float absquared = (float) (Math.pow(a.x-b.x, 2) + Math.pow(a.y-b.y, 2)); // Segment magnitude squared

        // Get the fraction for SphericalUtil.interpolate
        float fraction = dotproduct / absquared;

        if(fraction > 1) return segment.get(1);
        if(fraction < 0) return segment.get(0);
        return SphericalUtil.interpolate(segment.get(0), segment.get(1), fraction);
    }