Hi everybody
I’m having a little problem with some “Javascript Closure”, that I can’t seem to figure out.
I’m using a combination of PHP & Javascript to take twitter location variables & place them on a map. So far the PHP is fine, but the problem lies in that the window that is displayed (when the user clicks on a point) contains the last value associated with the variable (if that makes sense!) - the variable in question is “twittername”.
Here’s the code so far that’s run in a loop to plot the variables, usePointFromPostcode is called:-
function createMarker(point,html,icon) {
var marker = new GMarker(point,icon);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
function usePointFromPostcode(postcode, twittername) {
localSearch.setSearchCompleteCallback(null,
function() {
if (localSearch.results[0])
{
var resultLat = localSearch.results[0].lat;
var resultLng = localSearch.results[0].lng;
var point = new GLatLng(resultLat,resultLng);
var marker = new createMarker(point,twittername,icon);
map.addOverlay(marker);
}else{
alert("Postcode not found!");
}
});
localSearch.execute(postcode + ", UK");
}
What I am thinking of doing is instead of passing two variables to usePointFromPostcode, instead I pass through an array (or two arrays), containing all the values from postcode & twittername. Loop through the function multiple times, and display the results.
However, I’ve been unable to pass an array successfully into a function, can this be done?
You can see a (rough) development of my work here - http://rhyswynne.co.uk/ukseohere/
Any help would be greatfully appreciated!