need to remove last polyline and create current po

2019-07-19 18:29发布

i do google map with google direction i use class http://www.akexorcist.com/2015/12/google-direction-library-for-android-en.html that someone build for google direction. everything is work good, and the polyline show on the map. but when i need to click another direction like walking direction or drive direction i see 2 polyline on the map and i want to see only the current polyline.

here i create the polyline and remove the polyline in the start, then the polyline not show at all. i dont want that. i want when i click else button the polyline will remove. i try to put, polyline.remove(); on button click else but the problem that if, polyline.remove(); outside the parentheses, polyline.remove(); dont remove the polyline. i need to call polyline.remove(); when i click other button but i stuck. here is the code:

    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.when i click in walking button i want the transit polyline will remove that we have just all the time, only one polyline in the map. how i do that?

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

2条回答
The star\"
2楼-- · 2019-07-19 18:59

You can call mMap.clear(); after the button click. That method will remove all the polylines.

GoogleMap class documentation

UPDATE

You need to save the polylines to some list and then remove them when the button is clicked

private List<Polyline> polylines;

Add the polylines to the list

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

After button click

for (Polyline polyline : polylines) {
    polyline.remove();
}
查看更多
我命由我不由天
3楼-- · 2019-07-19 19:05

the better answer to this is you call following function on clear or whatever button/View click :

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();
        }
}
查看更多
登录 后发表回答