I am using Google Map to collect marker data to include in a form submission. My problem is, a user can add as many marker locations as they want before submitting. I would like to limit it so that only 1 marker can be added. Here'开发者_开发问答s my code:
 GEvent.addListener(map, "click", function(overlay, latlng) {
          if (latlng) {
            marker = new GMarker(latlng, {draggable:true});
            GEvent.addListener(marker, "click", function() {
              var html = "<table>" +
                         "<tr><td>Name:</td> <td><input type='hidden' id='name'/> </td> </tr>" +
                         "<tr><td>Address:</td> <td><input type='hidden' id='address'/></td> </tr>" +
                         "<tr><td>Type:</td> <td><select id='type'>" +
                         "</select> </td></tr>" +
                         "<tr><td></td><td></td></tr>";
              marker.openInfoWindow(html);
            });
            map.addOverlay(marker);
          }
        });
Simply define a global flag and set it when adding a marker:
var map;
var noMarker = true;
...
if (latlng && noMarker) {
...
map.addOverlay(marker);
noMarker = false;
OR:
myListener =  GEvent.addListener(map, "click", function(overlay, latlng) {
...  
map.addOverlay(marker);
GEvent.removeListener(myListener);
You could remove the event once the user has clicked (see the bottom of this page) - or alternately, you could use the stored marker variable and delete the old one anytime they click on your map to place a new one (depending on what you want to do).
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论