permanently deleting markers from an array

79 views Asked by At

I need to delete my markers with a right click because I have two buttons to show the markers and hide them but when I right click each marker its meant to delete it but it doesn't can anyone view my code and see what I missed because I can't see it and I have been looking for about 2 hours now:

function rightclickableMarker(marker, h) {
    google.maps.event.addListener(marker, 'rightclick', function(evt) {
        if(lastCoordinates[h] && lastCoordinates[h].setMap){
         lastCoordinates[h].setMap(null);
          delete (lastCoordinates);
          var idx = path.indexOf(lastCoordinates[h].getPosition())
          if (idx > -1) {
             path.splice(idx, 1);
            // removeLine();
             drawPath();
          }

        } 
    });
}
1

There are 1 answers

1
brenzy On

You can use "this" in you listener to delete the marker:

   google.maps.event.addListener(marker, 'rightclick', function(evt) {
     //...
     this.setMap(null);
     //...
   }

I suspect that lastCoordinates[h] doesn't actually contain the marker, but you haven't posted enough code to determine if this is the case. You also use lastCoordinates[h] after you have deleted it. Is it throwing an exception?