Sorry, my bad. The way to fix this problem is to pass in the Marker object into a function which returns a function which acts as the event handler and will get called when the 'click' event is fired.
Here is a similar thread http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/f40390e4a6ba89e9/25a246d92f93e9ea On Jul 30, 11:52 am, browndash <mabuz...@gmail.com> wrote: > Thanks. I tried that no luck; here is the edited code: > > var country_id = ''; > var country_name = ''; > > function createMarker(map, countries) { > > for (var i = 0; i < countries.length; i++) { > > if(countries[i].latitude != null && countries[i].longitude != > null) { > > var myLatlng = new > google.maps.LatLng(countries[i].latitude,countries[i].longitude); > var marker = new google.maps.Marker({ > position: myLatlng, > map: map, > icon: '/images/map-dot.png', > title: countries[i].name > }); > > country_id = countries[i].id; > country_name = countries[i].name; > google.maps.event.addListener(marker, 'click', function() { > getData(country_id, country_name); > }); > } > } > > } > > On Jul 30, 1:47 pm, Dmitry Mishchenko <dmitry.mishche...@gmail.com> > wrote: > > > > > Hey, > > Id is insufficiently scoped in regards to the click event, make it > > global > > > On Jul 30, 9:29 am, browndash <mabuz...@gmail.com> wrote: > > > > I am creating a custom map that shows all the countries and when you > > > click on a marker I would like to use Ajax to retrieve the information > > > for that country. When I first load the map, this is what I get from > > > the server: > > > > ..... > > > # > > > { > > > > * id: "11" > > > * name: "Algeria" > > > * longitude: "1.6596260" > > > * latitude: "28.0338860" > > > * type: "country" > > > * parent: "1" > > > > } > > > > # > > > - > > > { > > > > * id: "12" > > > * name: "Angola" > > > * longitude: "17.8738870" > > > * latitude: "-11.2026920" > > > * type: "country" > > > * parent: "1" > > > > } > > > > # > > > - > > > { > > > > * id: "13" > > > * name: "Benin" > > > * longitude: "2.3158340" > > > * latitude: "9.3076900" > > > * type: "country" > > > * parent: "1" > > > > } > > > > ......... > > > > And here is my code to create the markers from that JSON : > > > > function createMarker(map, countries) { > > > > for (var i = 0; i < countries.length; i++) { > > > > if(countries[i].latitude != null && countries[i].longitude != > > > null) { > > > > var myLatlng = new > > > google.maps.LatLng(countries[i].latitude,countries[i].longitude); > > > var marker = new google.maps.Marker({ > > > position: myLatlng, > > > map: map, > > > icon: '/images/map-dot.png', > > > title: countries[i].name > > > }); > > > > var id = countries[i].id; > > > google.maps.event.addListener(marker, 'click', function() { > > > getData(id); > > > }); > > > } > > > } > > > > } > > > > function getData(id) { > > > alert('Get data for this country:'+id);} > > > > ======================================================================== > > > > When I click on the marker this ID is always 230 (which is the last id > > > in my country list). What am I doing wrong here? How can I have a > > > unique argument for each marker when it calls the getData() function. -- You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group. To post to this group, send email to google-maps-js-api...@googlegroups.com. To unsubscribe from this group, send email to google-maps-js-api-v3+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.