deleting a marker after confirmation infowindow

后端 未结 1 1054
耶瑟儿~
耶瑟儿~ 2020-12-22 02:07

Yet another {\"error\": \"Please use POST request\"} from jsFiddle is thwarting my attempt to delete a google-maps-api-3 marker after getting confirmation from

相关标签:
1条回答
  • 2020-12-22 02:29

    A few rules to achieve what you want.

    1. Create only one instance of the infowindow object and use setContent() method to modify its content.

    2. Use the domready event of the infowindow to bind any action to an element within the infowindow.

    3. Add an Id to each marker so that you are able to identify each one separately. I used a simple counter in the below example. Increment it each time you create a marker.

    First create the infowindow instance and a counter:

    var infowindow = new google.maps.InfoWindow();
    var counter = 0;
    

    Then create each marker with a specific id and use that id on the infowindow button:

    function addMarker(location) {
    
        counter++;
    
        var marker = new google.maps.Marker({
            position: location,
            map: map,
            id: counter
        });
    
        markers.push(marker);
    
        var deleteButton = '<button id="deleteButton" data-id="' + counter + '">Delete</button>';
    
        google.maps.event.addListener(marker, 'rightclick', function () {
            infowindow.setContent(deleteButton);
            infowindow.open(map, marker);
        });
    }
    

    Then you need to listen to the domready event of the infowindow, call your delete function with the marker id that you get from the button, and finally loop through your markers array to delete the appropriate marker.

    JSFiddle demo

    0 讨论(0)
提交回复
热议问题