开发者

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(ev开发者_JS百科ent) {
    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.


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


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

  });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜