Apart from what the other answers indicate, you have another problem.
Your key
variable is scoped outside your inner event handler function. What happens is that each time you increment key
, you are changing the value that will be used in the event handler.
Effectively, let's say num
equals 10. All click event handlers will end up calling the following code:
function() {
marker[10].openInfoWindowHtml('xxxxxx');
}
One way to fix this is to scope the loop contents inside another function and invoke it immediately:
var num = rez.data.length;
var marker = [];
for(var key=0;key<num;key++)
{
var point = new GLatLng(rez.data[key].latitude, rez.data[key].longitude);
marker[key] = new GMarker(point, {icon: iconS});
function(key) {
GEvent.addListener(marker[key], "click", function() {
marker[key].openInfoWindowHtml('xxxxxx');
});
}(key);
map.getMap().addOverlay(marker[key]);
}
Edit: To clarify. What I do is that I declare an anonymous function, which I immediately invoke. Another way to see more clearly what is happening, the
function(key) {
GEvent.addListener(marker[key], "click", function() {
marker[key].openInfoWindowHtml('xxxxxx');
});
}(key);
can be replaced by:
function temp(key) {
GEvent.addListener(marker[key], "click", function() {
marker[key].openInfoWindowHtml('xxxxxx');
});
};
temp(key);
I.e., first declare a temporary function called temp
, then invoke it the row after.