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
On Oct 12, 7:20 am, tansaku <[email protected]> wrote:
> 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 <[email protected]> wrote:
>
> > 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
>
> > On Sep 6, 11:19 pm, Barry Hunter <[email protected]> wrote:
>
> > > Try using function closure
>
> > >http://econym.org.uk/gmap/closure.htm
>
> > > On 6 September 2010 20:25, eszpee <[email protected]> wrote:
>
> > > > 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
>
> > > > --
> > > > 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
> > > > [email protected].
> > > > To unsubscribe from this group, send email to
> > > > [email protected].
> > > > For more options, visit this group
> > > > athttp://groups.google.com/group/google-maps-js-api-v3?hl=en.
>
>
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-maps-js-api-v3?hl=en.