Remove the last plotted line from Google Map Android

让人想犯罪 __ 提交于 2020-01-06 06:59:09

问题


I have the following code which adds a PolylineOptions object to a Polyline array list. The argument passed in is a series of points returned from a query to the Directions API.

List<Polyline> polylines = new ArrayList<Polyline>();

public void drawPolylines(List<List<HashMap<String, String>>> result) {
    PolylineOptions lineOptions = null;
    // Traversing through all the routes
    for (int i = 0; i < result.size(); i++) {
        lineOptions = new PolylineOptions();
        // Fetching i-th route
        List<HashMap<String, String>> path = result.get(i);
        // Fetching all the points in i-th route
        for (int j = 0; j < path.size(); j++) {
            HashMap<String, String> point = path.get(j);
            double lat = Double.parseDouble(point.get("lat"));
            double lng = Double.parseDouble(point.get("lng"));
            LatLng position = new LatLng(lat, lng);
            lineOptions.add(position).width(6).color(Color.BLUE);
        }
        polylines.add(mMap.addPolyline(lineOptions));
    }
}

When I click on the undo button, I want it to remove the last line from the from the map, which is the last line on the polylines list. I have the following code, but nothing seems to be removed from the map whenever I run it. Is there anything else I need to add in?

    mButtonUndo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // create variable for the 2nd last point clicked and assign value form markerPoints array list
            LatLng lastPoint;
            lastPoint = markerPoints.get(markerPoints.size() - 2);

            // animate camera to centre on the previously touched position
            System.out.println("Centering camera to previous position at " + lastPoint.toString());
            mMap.animateCamera(CameraUpdateFactory.newLatLng(lastPoint));

            // remove line from the map
            for (int i = 0; i == polylines.size(); i++) {
                polylines.remove(polylines.size());
            }

            // remove value from the markerPoints array list

            // update the distance text
            double routeDistance = 0;
            distanceCount.remove(distanceCount.size() - 1);
            for (Double step : distanceCount) {
                routeDistance += step;
            }

            System.out.println("Total Distance calculated in undo in m = " + routeDistance);
            // output new value to ui
            mDistanceCount.setText(routeDistance / 1000 + "km");
        }
    });

回答1:


Found a solution by using a simple check in an enhanced for loop:

    mButtonUndo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // create variable for the 2nd last point clicked and assign value form markerPoints array list
            LatLng lastPoint;
            lastPoint = markerPoints.get(markerPoints.size() - 2);

            // animate camera to centre on the previously touched position
            System.out.println("Centering camera to previous position at " + lastPoint.toString());
            mMap.animateCamera(CameraUpdateFactory.newLatLng(lastPoint));


            // remove polyline object from the map
            for (Polyline line : polylines) {
                if (polylines.get(polylines.size() - 1).equals(line)) {
                    line.remove();
                    polylines.remove(line);
                }
            }

            // remove last value from the markerPoints array list
            markerPoints.remove(markerPoints.size() - 1);

            // update the distance text
            double routeDistance = 0;
            distanceCount.remove(distanceCount.size() - 1);
            for (Double step : distanceCount) {
                routeDistance += step;
            }

            System.out.println("Total Distance calculated in undo in m = " + routeDistance);
            // output new value to ui
            mDistanceCount.setText(routeDistance / 1000 + "km");
        }
    });



回答2:


you can clear the map using Map.clear();



来源:https://stackoverflow.com/questions/32024427/remove-the-last-plotted-line-from-google-map-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!