Can someone tell me why I can’t remove my polylines with this code:
$("#chkRouteLines").click(function () {
var polyline = new google.maps.Polyline({
path: positions,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2,
visible: true
}); ;
if ($(this).is(':checked')) {
polyline.setMap(map);
} else {
polyline.setMap(null);
}
})
I found in the documentation of google maps api 3 that I need to do: setMap(null)..
But this didn’t work.
Thank you!
You aren’t retaining a reference to the polyline, it is local to the “$(“#chkRouteLines”).click” function. If you have one polyline, make the reference to it global.
working fiddle
If you need to add and remove multiple different polylines, an array usually works.
code snippet: