3

I am getting the latitude and longitude on every onLocationChange and make a route like this:

public void onLocationChanged(Location location) {


      PolylineOptions rectLine2;
      rectLine2.add(new LatLng(lat, lon));
      mGoogleMap.addPolyline(rectLine2);
}

Now I want to clear this route on one click, and again when I move, a fresh route should draw, but in my case, old route doesn't clear.

I did the following but not working:

mGoogleMap.clear();// It clear's the map, but when I again draw the old route again comes.

Also

Polyline polyline;
polyline = mGoogleMap.addPolyline(rectLine2);
polyline.reomve()// This also didn't work.

Is there any other solution to clear the map?

Thanks In Advance!

Md. Monsur Hossain Tonmoy
  • 11,045
  • 2
  • 22
  • 19

5 Answers5

7

mGoogleMap.clear()

is used to clear the whole map so that you can redraw the polylines...

else you need to assign a variable to polyline and you can remove it...

PolylineOptions rectLine2;
rectLine2.add(new LatLng(lat, lon));
Polyline polyline = mGoogleMap.addPolyline(rectLine2);
polyline.remove();

These are the ways... Why do you need any other solution to clear..?

If you have number of polylines then just create an array and remove all polylines once you need to clear

0

Hmm, that's intresting, try this way, it's working for me:

private void clearPath() {
    if (polyline != null)
        polyline.remove();
    polyline = null;
}
romtsn
  • 11,704
  • 2
  • 31
  • 49
0

Try clearing your rectLine2 variable also. May be it's having all old latlangs. So on your click.

rectLine2 = new PolylineOptions();
MohK
  • 1,873
  • 1
  • 18
  • 29
0

mGoogleMap.clear() is the best way to clean the map. It removes all markers, polylines, polygons, overlays, etc from the map. see this http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#clear()

So the problem is not in that. You are doing something wrong in reDrawing the new path. I think you have not given all code in your onLocationChanged function. And if this is your final code than this should not work as you are only adding one point in the PolylineOptions. Please check this How to draw interactive Polyline on route google maps v2 android

Hope it will help

Community
  • 1
  • 1
Md. Monsur Hossain Tonmoy
  • 11,045
  • 2
  • 22
  • 19
0

Use map.clear() method to clear all map (polylines, markers, etc) or save all polylines somewhere and them remove them to save everything else

                    for(Polyline polyline : polylineList) {
                    polyline.remove();
                }
B-GangsteR
  • 2,534
  • 22
  • 34