And*_*eek 6 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.");
Run Code Online (Sandbox Code Playgroud)
请参考截图,需要根据当前位置更新折线。
您可以使用Google Maps Android API 实用程序库测试每个折线段的当前位置 PolyUtil.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;
}
}
...
Run Code Online (Sandbox Code Playgroud)
0然后像这样从路径中删除点ixLastPoint:
...
List<LatLng> pathPoints = path.getPoints();
for (int i = 0; i < ixLastPoint; i++) {
pathPoints.remove(0);
}
path.setPoints(pathPoints);
...
Run Code Online (Sandbox Code Playgroud)
注意!这只是方法,而不是完整的解决方案。