Google maps api and callbacks

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.
 
R

Richard Cornford

Lemon said:
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...
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.


All the function objects created to be call back functions share the
Activation/Variable object of the execution context in which they were
created, and it is that single Activation/Variable object that has the
- address - property representing the local variable. When your call
back functions come to act the value of the one - address - property of
the one Activation/Variable object the call back functions share has
the value last assigned to it; the value of the final address object
created.

But I cannot see a solution to achieve what I wanted to do.

Multiple Activation/Variable objects each referring to a different
address object. Something like:-

function doStuff():
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, getCallBackFor(address));
}
}

function getCallBackFor(address){
return (function(data, response){
getLocationsCallback(address, data, response);
});
}

Richard.
 
L

Lemon Tree

Richard said:
All the function objects created to be call back functions share the
Activation/Variable object of the execution context in which they were
created, ...
Thank you for your enlightening reply.
 
G

googleAcct

Lemon said:
Thank you for your enlightening reply.

and after answering the hard part, I can chime in with the easier part
- if those are the same addresses, definitely store them somehow rather
that looking them up each time.

In my app, I am mapping places that dont change, so I use a seperate
program ahead of time and geocode the addresses and add that to the
rest of the record.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top