469,576 Members | 1,819 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,576 developers. It's quick & easy.

Google maps api and callbacks

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[i].getAttribute("name");;
address.icon = markers[i].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.

Nov 13 '06 #1
3 9367
Lemon Tree wrote:
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...
<snip>
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[i].getAttribute("name");;
address.icon = markers[i].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.

<URL: http://www.jibbering.com/faq/faq_notes/closures.html >
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[i].getAttribute("name");;
address.icon = markers[i].getAttribute("icon");
geocoder.getLocations(address.name, getCallBackFor(address));
}
}

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

Richard.

Nov 13 '06 #2

Richard Cornford wrote:
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.

Nov 13 '06 #3

Lemon Tree wrote:
Richard Cornford wrote:
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.
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.

Nov 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by windandwaves | last post: by
6 posts views Thread by Sam Carleton | last post: by
17 posts views Thread by MeerkatInFrance | last post: by
1 post views Thread by xahlee | last post: by
5 posts views Thread by xml .NET group | last post: by
2 posts views Thread by H.Schmidt | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.