user547794
user547794

Reputation: 14521

Limit Google Maps V3 to 1 marker

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.

Upvotes: 2

Views: 1185

Answers (2)

yulerz
yulerz

Reputation: 66

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

Upvotes: 2

user547794
user547794

Reputation: 14521

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);

  });

Upvotes: 0

Related Questions