user3555472
user3555472

Reputation: 836

Android remove polyline from map v2

I am drawing a polyline on map on like an animation. like below.

m_handler = new Handler();
        m_handlerTask = new Runnable() {
            @Override
            public void run() {
                //line.remove();
                if (t < pointsPoly.size() - 1) {

                    LatLng src = pointsPoly.get(t);
                    LatLng dest = pointsPoly.get(t + 1);
                    Polyline lineAnimation = googleMap.addPolyline(new PolylineOptions()
                            .add(new LatLng(src.latitude, src.longitude),
                                    new LatLng(dest.latitude, dest.longitude))
                            .width(10).color(Color.DKGRAY).geodesic(true));
                    t++;

                } else {
                    t = 0;

                }
                m_handler.postDelayed(m_handlerTask, polyLineTimer);
            }
        };
        m_handler.post(m_handlerTask);

How can i remove the polyline? I don't want to clearMap(). I tried lineAnimation.remove(); but its not working.

Upvotes: 1

Views: 2483

Answers (1)

Jaythaking
Jaythaking

Reputation: 2102

You just do the following but instead of assign the result to a variable, put it in an ArrayList...

    ArrayList<Polyline> lines = new ArrayList<>();
    //Add line to map
    lines.add(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();

Removes this polyline from the map. After a polyline has been removed, the behavior of all its methods is undefined.

Upvotes: 4

Related Questions