Help on passing extra parameters to Geocoder

3,882 views
Skip to first unread message

eszpee

unread,
Sep 6, 2010, 3:25:04 PM9/6/10
to Google Maps JavaScript API v3
Hi,

I'm trying to pass extra parameters to a geocode lookup. I'm calling
lookups from a loop, and I'd like the callback function to know which
call's reply did it get.

The bare script can be seen in action here: http://underground.hu/temp/
- I tried to remove everything that's not related to the problem.

As you can see I'm trying to pass the variable 'i' to the callback
function, but it gets an 'undefined' value in the alerts.

Thanks a lot in advance for any help.

Peter Szasz

Barry Hunter

unread,
Sep 6, 2010, 5:19:41 PM9/6/10
to google-map...@googlegroups.com
Try using function closure

http://econym.org.uk/gmap/closure.htm

> --
> You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
> To post to this group, send email to google-map...@googlegroups.com.
> To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.
>
>

Barry Hunter

unread,
Sep 6, 2010, 5:20:59 PM9/6/10
to google-map...@googlegroups.com

geoco...@gmail.com

unread,
Sep 6, 2010, 6:00:07 PM9/6/10
to Google Maps JavaScript API v3
On Sep 6, 5:20 pm, Barry Hunter <barrybhun...@gmail.com> wrote:
> Also worth a read:http://code.google.com/apis/maps/documentation/geocoding/#Limits
> (the second paragraph)

As is this article on geocoding strategies:
http://code.google.com/apis/maps/articles/geocodestrat.html

-- Larry

eszpee

unread,
Sep 7, 2010, 6:36:06 AM9/7/10
to Google Maps JavaScript API v3
Thanks Barry, this helped!

PS: Thanks for the other info too, I'm not planning anything against
the TOS, it's going to be a one-time conversion tool for addresses to
help manual checking. The result will be displayed with a proper
Google map.

Thanks again.

eszpee

tansaku

unread,
Oct 12, 2010, 3:23:29 AM10/12/10
to Google Maps JavaScript API v3
Hi eszpee,

Sorry if you get this more than once - tried to send before and didn't
show up.

Sounds like the responses you got above helped you solve the problem.
I seem to be having the same issue. Any chance you could paste the
code you used to solve the problem, or a link to a working example?

Many thanks in advance
CHEERS> SAM

tansaku

unread,
Oct 12, 2010, 2:20:11 AM10/12/10
to Google Maps JavaScript API v3
Hi eszpee,

Any chance you can paste the code you used to get this working? I am
facing the exact same issue ...

CHEERS> SAM

On Sep 7, 7:36 pm, eszpee <esz...@gmail.com> wrote:

tansaku

unread,
Oct 19, 2010, 9:12:39 AM10/19/10
to Google Maps JavaScript API v3
I had email from eszpee off list and with his help solved my problem.
Yay! Thanks eszpee

And for anyone else stuck on this, the problem with eszpee's code
example above is that he is re-declaring the external function
variable i as a parameter in the new geocode callback function:

function convert_addresses() {
var addresses = document.forms.input.addresses.value.split('\n');
var i;
for (i = 0; i < addresses.length; i += 1) {
var actual_address = addresses[i];
geocoder.geocode( { 'address': actual_address}, function(results,
status, i) {
if (status == google.maps.GeocoderStatus.OK) {
alert(i+': '+results[0].formatted_address
+'\n'+results[0].geometry.location.toString()
+'\n'+results[0].geometry.location_type);
} else {
alert(i+': '+"Geocode was not successful for the following
reason: " + status);
}
});
}

}

The solution is to avoid the additional function argument. Without it
closure works as it should do with the internal function definition
picking up the external function variable i:

function convert_addresses() {
var addresses = document.forms.input.addresses.value.split('\n');
var i;
for (i = 0; i < addresses.length; i += 1) {
var actual_address = addresses[i];
geocoder.geocode( { 'address': actual_address}, function(results,
status) {
if (status == google.maps.GeocoderStatus.OK) {
alert(i+': '+results[0].formatted_address
+'\n'+results[0].geometry.location.toString()
+'\n'+results[0].geometry.location_type);
} else {
alert(i+': '+"Geocode was not successful for the following
reason: " + status);
}
});
}

}

As I understand it, with the additional argument i, i gets set to
"undefined" when the callback function is called and is passed only
the first two arguments ...

Hope that is helpful for others.

CHEERS> SAM
Reply all
Reply to author
Forward
0 new messages