I add polygon data on the Leaflet map by selecting parameters: travel_mode and travel_time from the form control and clicking a button "btnGenerate". This button click function is working as expected: it adds as many polygons as it is clicked. Say I click the button 3 times, with 3 different parameters of travel_mode and travel_time, it adds 3 different polygons on the map. Now when I click another button which invokes the function "removeTraveltime", it only removes the last added polygons. First 2 polygons are still on the map. My aim is to remove all 3 added polygons from the map with one click of the remove button. I am not sure how to implement that. My code is as below:
var ttPoly;
$("#btnGenerate").click(function(){
$.ajax({
url: 'php/traveltime.php',
type: 'POST',
data:{eno:enos,
travel_mode: $("#travelmode").val(),
travel_time: $("#traveltime").val()
},
success: callbackTT
});
function callbackTT(response){
ttPoly = L.geoJSON(JSON.parse(response), {forEach:function(ttPoly){
var poly =L.polygon(ttPoly.geometry.coordinates);
}});
ttPoly.setStyle({
color:'purple',
fillOpacity:0.4
});
$("#dlgTravelTime").hide();
ttPoly.addTo(mymap);
};
});
function removeTraveltime(enos){
if(ttPoly){
mymap.removeLayer(ttPoly);
}
}