【问题标题】:how to remove polyline path from source to current position while moving如何在移动时删除从源到当前位置的折线路径
【发布时间】:2018-11-23 12:21:53
【问题描述】:

我想在移动时删除之前的折线,我正在使用 GoogleDirection 类来绘制折线。请参考下面的代码sn-p

   GoogleDirection.withServerKey(getResources().getString(R.string.google_maps_key))
                .from(origin)
                .to(destination)
                .transportMode(TransportMode.DRIVING)
                .alternativeRoute(true)
                .execute(this);

 @Override
public void onDirectionSuccess(Direction direction, String rawBody) {   
            mMap.clear();
            mMap.addMarker(new MarkerOptions().position(destination));
            ArrayList<LatLng> directionPositionList = direction.getRouteList().get(0).getLegList().get(0).getDirectionPoint();
            polyline = mMap.addPolyline(DirectionConverter.createPolyline(this, directionPositionList, 3, Color.BLACK));

}
  @Override
public void onDirectionFailure(Throwable t) {
    Log.e("Direction fail Sorry", " no route found for this location.");

}

请参考截图,需要根据当前位置更新折线。

【问题讨论】:

  • 您找到解决方案了吗?

标签: android google-maps


【解决方案1】:

要删除折线,您只需使用 remove() 方法

//Add line to map
Polyline confirmPathPlotLine= mMap.addPolyline(new PolylineOptions()
            .add(new LatLng(location.getLatitude(), location.getLongitude()),
                    new LatLng(this.destinationLatitude, this.destinationLongitude))
            .width(1)
            .color(Color.DKGRAY)

//Remove the same line from map
confirmPathPlotLine.remove();

【讨论】:

    【解决方案2】:

    您可以测试每个多段线段在使用Google Maps Android API Utility Library 即PolyUtil.containsLocation(LatLng point, java.util.List&lt;LatLng&gt; polygon, boolean geodesic) 铺设的多段线段上的当前位置。

    ...
    int ixLastPoint = 0;
    for (int i = 0; i < path.getPoints().length; i++) {
        LatLng point1 = path.getPoints().get(i);
        LatLng point2 = path.getPoints().get(i);
        List<LatLng> currentSegment = new ArrayList<>();
        currentSegment.add(point1);
        currentSegment.add(point2);
        if (PolyUtil.isLocationOnPath(currentLocation, currentSegment, true, 50)) {
            // save index of last point and exit loop
            ixLastPoint = i;
            break;
        }
    }
    ...
    

    然后像这样从0 到ixLastPoint 的路径中删除点:

    ...
    List<LatLng> pathPoints = path.getPoints();
    for (int i = 0; i < ixLastPoint; i++) {
        pathPoints.remove(0);
    }
    path.setPoints(pathPoints);
    ...
    

    注意!这只是方法,不是完整的解决方案。

    【讨论】:

    • path 你是 PolylineOptions 还是 Polyline?
    • @Andrii Omelchenko 为什么你在 currentSegment (point1 & point2) 中添加了两次相同的点?
    • @BhavinChauhan 你找到相关的解决方案了吗?我知道已经一年了,对不起
    • @Fidel 我知道同一点使用两次,但神奇的是 Andrii 完整代码对我有用,谢谢 Andrii
    • 他提到了 containsLocation,但他使用 isLocationPath 代替。这导致寻求解决方案的人模棱两可。
    【解决方案3】:

    使用您的当前位置作为您的原点。然后每次 onLocationChange 给你 location clear ma​​p,并在 Direction API 下请求新的 Route。每次您请求新位置时,它也会请求新路线。

    【讨论】:

    • 解决方案会起作用,但它会多次请求路径,据我所知这是不好的方式
    猜你喜欢
    • 2020-04-12
    • 2011-11-15
    • 1970-01-01
    • 2017-12-04
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    相关资源
    最近更新 更多