I have a html form for adding new places in my data ,
I want the user add marker and delete last marker ,
I found this code but it is added more than one marker
How can make it delete the last one?
because I need just one marker? this is the demo of form http://saudi-hotels.info/add_hotel.php
<script type="text/javascript">
var map;
function mapa()
{
var opts = {'center': new google.maps.LatLng(26.12295, -80.17122),
'zoom':11, 'mapTypeId': google.maps.MapTypeId.ROADMAP }
map = new google.maps.Map(document.getElementById('mapdiv'),opts);
google.maps.event.addListener(map,'click',function(event) {
document.getElementById('long').value = event.latLng.lng();
document.getElementById('lat').value = event.latLng.lat();
marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(event.latLng.lat(),event.latLng.lng()),
});
}
);
}
</script>
please help me ,
You can create a global variable that tracks the last created marker:
And then, whenever you create a new marker, set
lastMarkerto the newly created marker:Then, whenever you want to remove the last created marker, you can use the
lastMarkervariable: