【问题标题】:need to remove last polyline and create current polyline需要删除最后一条折线并创建当前折线
【发布时间】:2016-10-09 17:40:12
【问题描述】:

我用谷歌方向做谷歌地图我使用类http://www.akexorcist.com/2015/12/google-direction-library-for-android-en.html 有人为谷歌方向构建。一切都很好, 并且折线显示在地图上。但是当我需要单击另一个方向(例如步行方向或行驶方向)时,我会在地图上看到 2 条折线,我只想看到当前的折线。

我在这里创建折线 并在开始时删除折线,然后折线根本不显示。 我不想要那个。我希望当我单击其他按钮时,折线将删除。 我试着把 polyline.remove();在按钮上单击其他,但如果,polyline.remove(); 的问题在括号外, polyline.remove();不要删除折线。我需要调用 polyline.remove();当我单击其他按钮但我卡住了。 这是代码:

    switch (direction.getStatus()) {
        case RequestResult.OK:
            Route route = direction.getRouteList().get(0);

            Leg leg = route.getLegList().get(0);

            ArrayList<LatLng> pointList = leg.getDirectionPoint();

            List<Step> stepList = direction.getRouteList().get(0).getLegList().get(0).getStepList();
            ArrayList<PolylineOptions> polylineOptionList = DirectionConverter.createTransitPolyline(this, stepList, 5, Color.RED, 3, Color.BLUE);
            for (PolylineOptions polylineOption : polylineOptionList) {
                polyline = mMap.addPolyline(polylineOption);
                polyline.remove();

            }

                break;

2.当我点击步行按钮时,我希望交通折线将被移除 我们一直都有,地图中只有一条折线。 我是怎么做到的?

   bWalking.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            if (locationGpsLatLng == null) {
                                                Toast.makeText(MapsActivity.this, "Need GPS location", `enter code here`Toast.LENGTH_SHORT).show();{
                                                    return;
                                                }
                                            }
                                            if (markerLocation == null) {
                                                Toast.makeText(MapsActivity.this, "Type on marker bathroom for destination", Toast.LENGTH_SHORT).show();
                                            } else {
                                            sendRequestWalkingDirection();


                                        }
                                    };
                                    private void sendRequestWalkingDirection() {
                                        GoogleDirection.withServerKey(APIKEY)
                                                .from(locationGpsLatLng)
                                                .to(markerLocation)
                                                .transportMode(TransportMode.WALKING)
                                                .execute(new DirectionCallback() {
                                                    @Override
                                                    public void onDirectionSuccess(Direction direction, String rawBody) {
                                                        Log.d("onDirectionSuccess", "status: " + direction.getStatus());
                                                        requestOk(direction, rawBody);
                                                        mMap.addMarker(new MarkerOptions().position(locationGpsLatLng).draggable(false).icon(BitmapDescriptorFactory
                                                                .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                                                        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(locationGpsLatLng, 15);
                                                        mMap.animateCamera(cameraUpdate);
                                                    }
                                                    @Override
                                                    public void onDirectionFailure(Throwable t) {
                                                        return;
                                                    }
                                                });
                                    }
   });

【问题讨论】:

    标签: android google-maps directions google-polyline


    【解决方案1】:

    您可以在点击按钮后拨打mMap.clear();。 该方法将删除所有折线。

    GoogleMap class documentation

    更新

    您需要将折线保存到某个列表中,然后在单击按钮时将其删除

    private List<Polyline> polylines;
    

    将折线添加到列表中

    polylines = new ArrayList<>();
    for (PolylineOptions polylineOption : polylineOptionList) {
        polyline = mMap.addPolyline(polylineOption);
        polylines.add(polyline);
    }
    

    点击按钮后

    for (Polyline polyline : polylines) {
        polyline.remove();
    }
    

    【讨论】:

    【解决方案2】:

    对此的更好答案是您在清除或任何按钮/查看点击时调用以下函数:

    public onClick(View v){
    if(googleMap!=null && polygon!=null && polyline!=null && polylineOptions!=null && 
                    polygonOptions!=null){
                polylineOptions.getPoints().clear();
                polygonOptions.getPoints().clear();
                googleMap.clear();
                polyline.remove();
                polygon.remove();
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      相关资源
      最近更新 更多