Google Maps Android API v2,如何从地图上移除多段线?

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);
    }

}

1
根据文档,“remove()”应该是正常工作的。确保你正在使用 Extras 区域中 SDK 管理器中的“Google Play 服务”库的最新版本(当前最新版本为“rev 5”),并且你正在使用该库项目。如果问题仍然存在,请创建一个演示错误的示例项目,可以将其作为问题发布到地图问题跟踪器:http://code.google.com/p/gmaps-api-issues/issues/list - CommonsWare
谢谢,我会尝试。 - DeadManSpirit
我需要单独应用更改到地图上吗(调用一个函数)? - DeadManSpirit
据我所知,没有。 - CommonsWare
更新“Google Play服务”库解决了问题,谢谢。 - DeadManSpirit
3个回答

47

要删除折线,您只需像 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();

我遇到了类似的困难。我在折线上调用了setPoints,但仍然存在从先前的点中未被移除的痕迹。这似乎是一个时间问题,因为我更新点的速度越快,影响就越严重。 - Howard Swope

1

最简单的方法是这样做

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 = "YOUR_API_KEY";
      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();

                    }
                });

新的LatLng(31.345394,73.429810));


0
在地图活动中创建一个折线的成员变量。
    Polyline polyline_final = null;

每次在地图上添加折线时,都将地图的值分配为这样
polyline_final = mMap.addPolyline(polylineOptions);

同时创建新的折线并从地图中删除先前的折线,在执行以下条件之前应用此操作: polyline_final = mMap.addPolyline(polylineOptions);

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

现在已经完成了


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接