Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 969
user1454212 Avatar asked Jun 13 '12 17:06

user1454212


People also ask

Is there a drawing tool on Google Maps?

Add line or shape.Select a layer and click where to start drawing. A layer can have 2,000 lines, shapes or places. Click each corner or bend of your line or shape. To move the map, click and hold the mouse.

How do I draw on Google Maps app?

To draw a route, click "Add directions," choose transportation mode, and enter start and end points. You can draw lines and shapes on maps by clicking "Draw a line" and selecting "Add line or shape."


1 Answers

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);
      }
    });
  }
like image 77
Heitor Chang Avatar answered Sep 23 '22 16:09

Heitor Chang