I create a marker on the map, in the same function there is a function of deleting a marker by right-click (well, or a separate one that checks by id
, the problem will be the same).
google.maps.event.addListener(map, 'click', function (event) { var marker = new google.maps.Marker({ position: event.latLng, map: map, draggable: true }); markers.push(marker); google.maps.event.addListener(marker, "rightclick", function (event) { markers.splice(marker, 1); marker.setMap(null); }); });
Also there is a function to delete all at once.
var deleteMarkers = function () { markers.forEach(function (marker) { marker.setMap(null); }); markers = []; }
There is no problem if you delete all the markers or one by one, or leave 2 or more. But if you leave 1, and then delete via deleteMarkers
, then the marker remains on the map, the array is cleared.