Synchronous reverse geocoding

1,668 views
Skip to first unread message

Rob

unread,
Nov 4, 2011, 8:44:58 PM11/4/11
to Google Maps JavaScript API v3
Hi everyone,

I have a map where I drop a number of markers. I need to save the
location of the markers to a database, along with the city and country
that the marker was placed in.
I keep track of all the markers in an array, hence, when I need to
save all the markers to the database through an AJAX call, I iterate
through the array, and establish a reverse geocoding request to get
the city and country of each marker.
The issue is that because geocoding requests are an AJAX call, the
city/country data that is sent to the backend is null, since it does
not wait for the reverse geocoding request to be completed.

Any way I can do this synchronously?

Rossko

unread,
Nov 4, 2011, 9:46:45 PM11/4/11
to Google Maps JavaScript API v3
> I have a map where I drop a number of markers.

That would be the time to do reverse gecoding?

>  I need to save the
> location of the markers to a database, along with the city and country
> that the marker was placed in.

Beware the terms of use for storing Google's data

> Any way I can do this synchronously?

No, but you can write code that simulates it e.g. execute one request,
when response comes execute the next, when all complete do whatever
else. The next pitfall will be the limited rate of request allowed,
to prevent abuse.
Message has been deleted

Rob

unread,
Nov 5, 2011, 3:34:10 AM11/5/11
to Google Maps JavaScript API v3
Thanks for helping out, so I followed your suggestion of doing a
reverse geocode when dropping the marker, such as:

markerLocation = reverseGeocode(marker.getPosition());
alert(markerLocation.city);

However, markerLocation is always null, even though within the
reverseGeocode function, the city is being populated properly. I am
assuming this is because JS doesn't wait for the AJAX request to be
completed, and just continues executing the rest of the code. Any
idea on how to solve this?

geoco...@gmail.com

unread,
Nov 5, 2011, 3:36:50 AM11/5/11
to Google Maps JavaScript API v3
On Nov 5, 12:34 am, Rob <rob.feli...@gmail.com> wrote:
> Thanks for helping out, so I followed your suggestion of doing a
> reverse geocode when dropping the marker, such as:
>
>                         markerLocation = reverseGeocode(marker.getPosition());
>                         alert(markerLocation.city);
>
> However, markerLocation is always null, even though within the
> reverseGeocode function, the city is being populated properly.  I am
> assuming this is because JS doesn't wait for the AJAX request to be
> completed, and just continues executing the rest of the code.  Any
> idea on how to solve this?

the reverse geocoder is asynchronous, you can't return a value from it
like that.

set the position of the marker in the callback routine.

-- Larry

Rob

unread,
Nov 5, 2011, 6:04:02 PM11/5/11
to Google Maps JavaScript API v3
Thanks Larry, in case anyone gets this issue, this is what I did to
fix it:

//takes a google.LatLng object
//returns an array with city at [0] amd country at [1]
function reverseGeocode(marker) {

var location = new Object();
city = ["administrative_area_level_1", "political"];
country = ["country", "political"];

var geocoder = new google.maps.Geocoder();
geocoder.geocode({location: marker.getPosition()}, function(results,
status) {
if (status == google.maps.GeocoderStatus.OK) {
for(i=0; i < results[0].address_components.length; i++) {
if((location.city && location.country) == null) {
if(results[0].address_components[i].types[0] == city[0] &&
location.city == null) {
location.city =
results[0].address_components[i].long_name;
}
else if(results[0].address_components[i].types[0] == country[0]
&& location.country == null) {
location.country = results[0].address_components[i].long_name;
}
}
}
marker.city = location.city;
marker.country = location.country;
}
});
}

On Nov 5, 12:36 am, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages