2

I had code that was working for map V2 but have since upgraded to V3 and the constructors are different. I want to limit my map to 1 marker only. Here's the code to add a marker:

myListener = google.maps.event.addListener(map, 'click', function(event) {
    placeMarker(event.latLng);
  });

....

 function placeMarker(location) {
  var marker = new google.maps.Marker({
      position: location, 
      map: map,
      draggable: true
  });

  map.setCenter(location);

I need code that will remove myListener once a marker is placed. Thanks for any help you can provide.

user547794
  • 14,263
  • 36
  • 103
  • 152

2 Answers2

2

try google.maps.event.addListenerOnce() method instead

yulerz
  • 66
  • 3
0

I used this code and it did the trick:

    myListener = google.maps.event.addListener(map, 'click', function(event) {
    placeMarker(event.latLng), google.maps.event.removeListener(myListener);

  });
user547794
  • 14,263
  • 36
  • 103
  • 152