I think this is a scale problem. Since the event fires after I have added all the listeners, num_markers has always been overridden by the next loop in the loop.
Is there a way to pass variables to event functions?
I tried this approach, but it is not for me. Google Maps: Event Listener remembers only the final value of a variable
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); var info_window = new google.maps.InfoWindow(); var markers = []; function load_markers() { var bounds_url = map.getBounds().toUrlValue(); $.ajax({ url:'/retailer-markers?bounds='+bounds_url, dataType: 'json', success: function(data) { for(i = 0; i < data.length; i++) { var marker_pos = new google.maps.LatLng(data[i]['lat'], data[i]['long']); //Every time the listener event is called this number is the length of the array var marker_num = get_markers_count(); markers[marker_num] = new google.maps.Marker({ position: marker_pos, map: map, title:data[i]['Title'], icon: image }); google.maps.event.addListener(markers[marker_num], 'click', function() { info_window.setContent('hello'); var pos = markers[marker_num].getPosition(); info_window.setPosition(pos); info_window.open(map, markers[marker_num]); }); } } }); }
javascript scope google-maps google-maps-api-3
Keyo
source share