18

我正在尝试删除以前添加的折线并在位置更改后重新绘制新的折线。我都试过了

this.routeToDestination.setPoints(pointsToDestination) 和 this.routeToDestination.remove()

但他们都没有工作。

我关注了如何使用 Google Maps Android API v2 绘制动态线(路线),但无法解决问题

    @Override
    public void onResume() {
        super.onResume();

        routeToDestination = mMap.addPolyline(new PolylineOptions()
                .add(new LatLng(location.getLatitude(), location.getLongitude()),
                        new LatLng(this.destinationLatitude, this.destinationLongitude))
                .width(1)
                .color(Color.DKGRAY)

        );
    }

   @Override
    public void onLocationChanged(Location location) {

        List<LatLng> pointsToDestination = new ArrayList<LatLng>();
        pointsToDestination.add(new LatLng(location.getLatitude(), location.getLongitude()));
        pointsToDestination.add(new LatLng(destinationLatitude, destinationLongitude));

        this.routeToDestination.setPoints(pointsToDestination);
    }

}
4

3 回答 3

46

要删除折线,您应该简单地使用 API 中所述的 remove() 方法。

//Add line to map
Polyline line = 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
line.remove();
于 2014-05-17T21:34:54.263 回答
0

在折线的地图活动中创建成员变量

    Polyline polyline_final = null;

每次将折线添加到地图时,都会像这样分配地图的值

polyline_final = mMap.addPolyline(polylineOptions);

接下来制作新的折线同时删除以前的折线形式地图在polyline_final = mMap.addPolyline(polylineOptions);之前应用此if条件

if (polylineOptions_final!=null)
                        {
                            polylineOptions_final.remove();
                        }

现在完成了

于 2021-04-25T06:43:46.300 回答
0

最简单的方法

    implementation 'com.akexorcist:google-direction-library:1.2.1'

使用这个库获取一个 Api 密钥

https://developers.google.com/maps/documentation/directions/get-api-key

您必须为方向 api 启用此计费方式。

https://console.cloud.google.com/projectselector2/billing/enable

在任何函数中设置此代码,但不在 onmapready 函数中

 String serverKey = "AIzaSyCHEn5TVLk6xxuYn0-g11xxVzCu9eZiVbU";
      GoogleDirection.


        GoogleDirection.withServerKey(serverKey)
                .from(new LatLng(30.677717,73.106812))
                .to(new LatLng(31.345394,73.429810))
                .transitMode(TransportMode.WALKING)
                .unit(Unit.METRIC)
                .execute(new DirectionCallback() {
                    @Override
                    public void onDirectionSuccess(@Nullable Direction direction) {


                        Log.d("eeeeee", direction.getStatus());

                        if(direction.isOK()) {
                            // Do something
                            Route route = direction.getRouteList().get(0);
                            Leg leg = route.getLegList().get(0);

                            ArrayList<LatLng> directionPositionList = leg.getDirectionPoint();
                            Log.d("eeeeee", directionPositionList.size()+"eeeeee");
                            if (polylineOptions_final!=null)
                            {
                                polylineOptions_final.remove();
                            }
                            PolylineOptions polylineOptions = DirectionConverter.createPolyline(getApplication(), directionPositionList, 10, Color.RED);
                            polylineOptions_final = mMap.addPolyline(polylineOptions);
                        } else {
                            // Do something
                        }
//                        Log.d("eeeeee"+direction.getGeocodedWaypointList().toString(), "onDirectionSuccess: ");


                    }

                    @Override
                    public void onDirectionFailure(@NonNull Throwable t) {
                        Toast.makeText(ClientMap.this, "d"+t.getMessage().toLowerCase(), Toast.LENGTH_SHORT).show();

                    }
                });

新纬度(31.345394,73.429810));

于 2021-04-25T06:50:47.927 回答