By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

Google maps api and callbacks

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a

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

P: n/a

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.