I have some problems with my code, I have a list of airports in a sql database, and I want to create markers for each 1 of those airports.
For the address i got ICAO-codes for each airport, an ICAO is unique for each airport
I get the data from the Database as array
it's is saved in "temp" with an split function and with the for-loop it get them 1 by 1
Geocoding is not the problem, but I don't know why for the TITLE and the on click event it is always the last one from the array which is used.
here is the page, the last entry in the database is ZBAA.
And all the markers are placed at the correct location but the title is wrong :s
http://mizar.lte.lu/~pr1011_meteo/projet/cartemonde4.php
The problem is with "address" i think but i m not sure.
for (var i = 0; i < temp.length; ++i){
var address=temp[i];
geocoder.geocode({ 'address': address}, function(results){
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title:address
});
google.maps.event.addListener(marker, 'click', function() {
window.open ('infomonde.php?icao='+address+'&language=fr', 'Informations météo', config='height=400, width=850, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no')});
});
};
Here is a JSFiddle Demo using "dummy" addresses and alert to show the correct data associate with each marker:
What you have is a typical closure/scope issue within the for loop. To fix the issue use closure to localize the temp[i]
variable before passing into geocode and callback function within it:
for (var i = 0; i < temp.length; ++i) {
(function(address) {
geocoder.geocode({
'address': address
}, function(results) {
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: address
});
google.maps.event.addListener(marker, 'click', function() {
//alert(address); //use alert to debug address
window.open('infomonde.php?icao=' + address + '&language=fr', 'Informations météo', config = 'height=400, width=850, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no')
});
});
})(temp[i]); //closure passing in temp[i] and use address within the closure
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With