user1454212
user1454212

Reputation: 449

Google Maps API v3: Drawing Manager

I am working with the Drawing Manager in the Drawing Library and a question arose. Any help would be greatly appreciated. Thanks in advance.

Question: After an object (marker, circle, etc...) is created, how would I call it? An example would be that I placed a marker. I now want to attach an info window to it. In the function to assign an info window, I need the "name" of the marker that i just placed.

Let me know if you need any more clarification.

-Seth

Upvotes: 5

Views: 7735

Answers (1)

Heitor Chang
Heitor Chang

Reputation: 6057

You can use an event listener to obtain a reference to the created object (event.overlay). In this demo, the created markers are made to open the InfoWindow with content stored in the marker itself.

Click to create markers, then switch to the "Hand" icon mode and click on markers to open the InfoWindow.

  var markers = [];
  var infowindow = new google.maps.InfoWindow();

  function initialize() {
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    drawingManager.setMap(map);
    google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
      if(event.type == google.maps.drawing.OverlayType.POLYLINE) {
        alert("polyline complete");
      }
      else if(event.type == google.maps.drawing.OverlayType.MARKER) {
        var newMarker = event.overlay;
        newMarker.content = "marker #" + markers.length;
        google.maps.event.addListener(newMarker, 'click', function() {
          infowindow.setContent(this.content);
          infowindow.open(map, this);
        });
        markers.push(newMarker);
      }
    });
  }

Upvotes: 5

Related Questions