0

I have a problem with google maps V3 Code is part of example from google. I want to add listener to EACH marker, so I set marker as array. But its not working :( can anyone help me?

    function initialize() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);

    // Add 10 markers to the map at random locations
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    var lngSpan = northEast.lng() - southWest.lng();
    var latSpan = northEast.lat() - southWest.lat();
    var marker = new Array(10); 
    for (var i = 0; i < 10; i++) {
      var latlng = new GLatLng(southWest.lat() + latSpan * Math.random(),
                              southWest.lng() + lngSpan * Math.random());

      marker[i] = new GMarker(latlng,{ draggable: true });

      GEvent.addListener(marker[i] , "dragstart", function() {
        map.closeInfoWindow();
      });

      GEvent.addListener(marker[i]  , "dragend", function() {
        marker[i].openInfoWindowHtml("text" + i);
      });

       map.addOverlay(marker[i] );  
    }
  }
}
Galen
  • 29,976
  • 9
  • 71
  • 89
Marek
  • 1
  • when I want to drag marker, I can move it. But after finished moving, I want to show text. but nothing happens. If it will work corretly, in the bottom of marker is little cross, which dissapear after moving. Now, the cross are still on the map. – Marek Nov 15 '10 at 18:00
  • here is an example - watch only map please, its only part of page.. http://praha-cyklistika.cz/map2.html – Marek Nov 15 '10 at 18:07
  • Take a look at closures and try to understand the use of them.I think it might help – Argiropoulos Stavros Nov 15 '10 at 19:51
  • many thanks, thats what im looking for... – Marek Nov 15 '10 at 22:06

2 Answers2

0

Google Maps v2 API is deprecated, you should use v3. See this working example using the new API: Google Maps JS API v3 - Simple Multiple Marker Example

Community
  • 1
  • 1
stealthyninja
  • 10,343
  • 11
  • 51
  • 59
0

This looks like Google maps api V2 code.

  • Replace GLatLng with google.maps.LatLng,
  • Replace GEvent with google.maps.event
  • Instead of map.addOverlay(marker[i]);, do marker[i].setMap(map); in V3.

For the rest, make sure to read the API documentation. There are lots of examples there that'll help you get going.

Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122