【问题标题】:Google Maps Android API v2, how to remove Polylines from the map?Google Maps Android API v2,如何从地图中删除折线?
【发布时间】:2013-02-28 15:24:59
【问题描述】:

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

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

但他们都没有工作。

我关注了How to draw a dynamic line (route) with 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);
    }

}

【问题讨论】:

  • 根据文档, remove() 应该可以工作。确保您在 SDK 管理器的 Extras 区域中使用最新版本的“Google Play 服务”库(现在最新的是“rev 5”)并且您正在使用该库项目。如果问题仍然存在,请创建一个示例项目来展示错误并将其发布,可能作为地图问题跟踪器的问题:code.google.com/p/gmaps-api-issues/issues/list
  • 谢谢,我去试试
  • 我是否必须单独对地图应用更改(调用函数)?
  • 据我所知没有。
  • 更新“Google Play 服务”库解决了这个问题,谢谢

标签: android google-maps-android-api-2


【解决方案1】:

要删除折线,您只需使用 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,但仍然有一些工件永远不会从以前的点中删除。这似乎是一个时间问题,因为我更新点的速度越快效果越差。
【解决方案2】:

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

    Polyline polyline_final = null;

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

polyline_final = mMap.addPolyline(polylineOptions);

和下一步同时制作新的折线删除以前的折线形式地图应用这个if条件之前 polyline_final = mMap.addPolyline(polylineOptions);

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

现在完成了

【讨论】:

    【解决方案3】:

    最简单的方法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      相关资源
      最近更新 更多