移动时如何从源到当前位置删除折线路径

时间:2018-11-23 12:21:53

标签: android google-maps

我想在移动时删除上一条折线,我正在使用GoogleDirection类绘制折线。请参考下面的代码片段

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

enter image description here     }

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

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

您可以为每个折线段测试Google Maps Android API Utility LibraryPolyUtil.containsLocation(LatLng point, java.util.List<LatLng> 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;
    }
}
...

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

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

NB!这只是方法,而不是完整的解决方案。

答案 2 :(得分:0)

使用当前位置作为您的来源。然后每次 onLocationChange 给您位置 清除 地图,并在Direction API下请求新的Route。每当您请求新位置时,它也会请求新路线。