Store Geocoder results in JavaScript array

460 views
Skip to first unread message

Tatiana Kazantseva

unread,
Nov 4, 2010, 3:14:43 AM11/4/10
to google-map...@googlegroups.com
Is it possible to store the results produced by Geocoder request in a global array? Let's say, I pass an array containing addresses, and I want LatLng results to be stored in a new global array outside the geocode function?

Chris Broadfoot

unread,
Nov 4, 2010, 3:25:24 AM11/4/10
to google-map...@googlegroups.com
Of course. The return result from the Geocoder is just a JavaScript object.

Just assign it to a global variable like you would any other JS value.

On Thu, Nov 4, 2010 at 6:14 PM, Tatiana Kazantseva <kazan...@gmail.com> wrote:
Is it possible to store the results produced by Geocoder request in a global array? Let's say, I pass an array containing addresses, and I want LatLng results to be stored in a new global array outside the geocode function?

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

Tatiana Kazantseva

unread,
Nov 4, 2010, 3:31:17 AM11/4/10
to google-map...@googlegroups.com
I did that. I find coordinates, I use push to add them to global array, I alert results and everything is fine. But when I check my array outside the function, it's empty

Chris Broadfoot

unread,
Nov 4, 2010, 3:41:30 AM11/4/10
to google-map...@googlegroups.com
Is your array really global? Are you by chance using the 'var' keyword in a non global context?

Could you provide a link to your code?

Also, more likely than not, you don't actually need to have variables globally accessible. Sure, it's handy -- but pollutes the global namespace and can lead to conflicts.

Rossko

unread,
Nov 4, 2010, 6:39:08 AM11/4/10
to Google Maps JavaScript API v3
> I did that. I find coordinates, I use push to add them to global array, I
> alert results and everything is fine. But when I check my array outside the
> function, it's empty

geocoding is asynchronous. You are probably checking the array before
the data has actually returned. But that's only a guess , follow the
posting guidelines if you want more help.

Tatiana Kazantseva

unread,
Nov 4, 2010, 5:44:59 PM11/4/10
to google-map...@googlegroups.com

Rossko

unread,
Nov 4, 2010, 7:12:43 PM11/4/10
to Google Maps JavaScript API v3
> http://kazantsevat.com/mashups/meetup/index2.php

Geocoding is asynchronous.
getCoordinates() fires off a geocoding request.
It does not wait for the response.
It returns to the calling code and
alert (coord);
is executed before the results have returned.

Some time later, the geocoding results return and are processed.

If you want to do something with the geocding results, you must do it
inside the geocoder callback function - same way as you are placing a
marker now
Reply all
Reply to author
Forward
0 new messages