Reputation: 14521
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);
}
});
Upvotes: 0
Views: 160
Reputation: 38135
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);
Upvotes: 1
Reputation: 15526
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).
Upvotes: 0