L
Lemon Tree
Hi everybody.
I am new to Javascript so probably I am asking something trivial, but I
cannot see where there problem is.
Or, better. Maybe I know where the problem is but I cannot find a
solution. So, here we go...
I have loaded an XML document containing a bunch of addresses.
For each address I would like to put a marker on a map using the Google
Maps API. In order to do so, I iterate over the address tags in the XML
file and for every address I create an object whose fields are the
address to look for (name) and the id of an icon to use for the marker
(icon).
At every iteration I call the getLocation method of GClientGeocoder
that performs the search, asynchronoulsy. This method requires a string
representing the address and a function that will be called as a
callback when the search is over. The callback function accepts 2
parameters, the data representing all the found information with
respect to the searched address and a responseCode.
Now, I would like to pass to the callback also the allocated address
object. In this way, when a serach is over I can easily retrieve the
icon associated with the looked-up address. In order to do so I pass to
the getLocation method a closure that simply forwards the geocoder
results to another function that accepts also a third parameter that is
the address object.
Here it is the code:
var markers = xml.documentElement.getElementsByTagName("address");
for(var i = 0; i < markers.length; i++) {
var address = new Object();
address.name = markers.getAttribute("name");;
address.icon = markers.getAttribute("icon");
geocoder.getLocations(address.name, function(data, response) {
getLocationsCallback(address, data, response);
}
);
}
Now the problem is the following:
Suppose that I have an xml with 4 addresses A1, A2, A3, A4.
Everything works well, getLocationCallback is called 4 times. Data and
response parameters correctly contain the found map data... BUT...
address is always A4 at every call!!!
Maybe the problem here is with variable scoping and parameter passing.
But I cannot see a solution to achieve what I wanted to do.
Does anyone have an idea?
Thank you for your help.
I am new to Javascript so probably I am asking something trivial, but I
cannot see where there problem is.
Or, better. Maybe I know where the problem is but I cannot find a
solution. So, here we go...
I have loaded an XML document containing a bunch of addresses.
For each address I would like to put a marker on a map using the Google
Maps API. In order to do so, I iterate over the address tags in the XML
file and for every address I create an object whose fields are the
address to look for (name) and the id of an icon to use for the marker
(icon).
At every iteration I call the getLocation method of GClientGeocoder
that performs the search, asynchronoulsy. This method requires a string
representing the address and a function that will be called as a
callback when the search is over. The callback function accepts 2
parameters, the data representing all the found information with
respect to the searched address and a responseCode.
Now, I would like to pass to the callback also the allocated address
object. In this way, when a serach is over I can easily retrieve the
icon associated with the looked-up address. In order to do so I pass to
the getLocation method a closure that simply forwards the geocoder
results to another function that accepts also a third parameter that is
the address object.
Here it is the code:
var markers = xml.documentElement.getElementsByTagName("address");
for(var i = 0; i < markers.length; i++) {
var address = new Object();
address.name = markers.getAttribute("name");;
address.icon = markers.getAttribute("icon");
geocoder.getLocations(address.name, function(data, response) {
getLocationsCallback(address, data, response);
}
);
}
Now the problem is the following:
Suppose that I have an xml with 4 addresses A1, A2, A3, A4.
Everything works well, getLocationCallback is called 4 times. Data and
response parameters correctly contain the found map data... BUT...
address is always A4 at every call!!!
Maybe the problem here is with variable scoping and parameter passing.
But I cannot see a solution to achieve what I wanted to do.
Does anyone have an idea?
Thank you for your help.