Reverse Geocoding does not works if I return the result in a function

269 views
Skip to first unread message

thebit

unread,
Jul 18, 2011, 6:44:42 PM7/18/11
to Google Maps JavaScript API v3
Hello,
I have the following problem: I wrote a function that converts
coordinates into formatted address via geocode and I put the code in a
function (in way to reutilise that).
*************************
function coordinates2Address(latLng)
{
var geocoder;
var codedAddress;
geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': latLng}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
if (results[1])
{
codedAddress = results[1].formatted_address;
alert(codedAddress);
}
else
{
codedAddress = "No data";
}
return codedAddress;
}
else
{
alert("Geocoder failed due to: " + status);
}
});
}
*********************

If I print the value with an alert, it works. If I print the result by
a jQuery append (by appending the value returned) no dat is showing,
but only the text "undefined".
Why?
Thanks.

geoco...@gmail.com

unread,
Jul 18, 2011, 7:25:21 PM7/18/11
to Google Maps JavaScript API v3
On Jul 18, 6:44 pm, thebit <the-...@email.it> wrote:
> Hello,
> I have the following problem: I wrote a function that converts
> coordinates into formatted address via geocode and I put the code in a
> function (in way to reutilise that).
> *************************


>
> If I print the value with an alert, it works. If I print the result by
> a jQuery append (by appending the value returned) no dat is showing,
> but only the text "undefined".
> Why?

You can't do that. Geocoding and reverse geocoding are asynchronous
(they send a query to google's server and the callback function runs
when the data comes back).

Any data must be used in the callback function.

-- Larry

> Thanks.

thebit

unread,
Jul 19, 2011, 3:08:52 AM7/19/11
to Google Maps JavaScript API v3
Hello,
thank your for your answer.
Please, can you give me a little example?
I have a lot of data (markers) to geocode from coordinates to address
and I'm looking for a dynamic way to do that.
Thanks.

Rossko

unread,
Jul 19, 2011, 4:51:43 AM7/19/11
to Google Maps JavaScript API v3
> I have a lot of data (markers) to geocode from coordinates to address
> and I'm looking for a dynamic way to do that.

Have a read of
http://code.google.com/apis/maps/articles/geocodestrat.html
before you decide how to go about that.

thebit

unread,
Jul 19, 2011, 5:20:00 AM7/19/11
to Google Maps JavaScript API v3
I think I will utilise client-side geocoding (such as the way I
utilise now).
I do not think (I hope!) there will be some users who will made more
thant 2.500 requests of revers geocoding per day.
However, how I can to do to solve my problem by using a callback
function?
Thanks.

On 19 Lug, 10:51, Rossko <ros...@culzean.clara.co.uk> wrote:
> > I have a lot of data (markers) to geocode from coordinates to address
> > and I'm looking for a dynamic way to do that.
>
> Have a read ofhttp://code.google.com/apis/maps/articles/geocodestrat.html

geoco...@gmail.com

unread,
Jul 19, 2011, 6:44:11 AM7/19/11
to Google Maps JavaScript API v3
On Jul 19, 5:20 am, thebit <the-...@email.it> wrote:
> I think I will utilise client-side geocoding (such as the way I
> utilise now).
> I do not think (I hope!) there will be some users who will made more
> thant 2.500 requests of revers geocoding per day.
> However, how I can to do to solve my problem by using a callback
> function?

What are you doing now?
If you provide a link to your map (as requested in the posting
guidelines) someone might be able to provide direction on how to
modify it.

The general direction is rather than returning a value from the
function, use the callback routine to do whatever you need to do with
the returned value. I am guessing you need to send the data back to
your server to be stored in a database.

-- Larry

thebit

unread,
Jul 19, 2011, 6:51:53 AM7/19/11
to Google Maps JavaScript API v3
>  I am guessing you need to send the data back to
> your server to be stored in a database.

No, I only stored the lat, lng coordinates in my DB. The reverse
geocoding is a service I wish to offer to know (by mouse over) the
address for each lat&lng listed.
I asked an example because I have an application to complex to upload
for a simple testing.
However, if you want it I must to extrapolate the code I use about the
geocode.
Meanwhile can you post a example of coordinates2Address in way to
correctly works?
Thanks.

thebit

unread,
Jul 19, 2011, 7:08:29 AM7/19/11
to Google Maps JavaScript API v3
I uploaded a simple test page here: http://thebit.altervista.org/maps/testAddMarker23_web.php
If you try: the result is showed in alert window but not in
"risultato" div.

geoco...@gmail.com

unread,
Jul 19, 2011, 7:17:18 AM7/19/11
to Google Maps JavaScript API v3

geoco...@gmail.com

unread,
Jul 19, 2011, 7:25:53 AM7/19/11
to Google Maps JavaScript API v3
1. Don't use asynchronous services in a tight loop, it might work for
a few, but will eventually run into quota/rate limits.
2. The reason the result appears in the alert is that the alert is in
the callback routine, sequence of events:
a. call function
b. function sends request to server
c. function ends (no return value specified)
d. data comes back from server
e. callback function runs, displaying the alert

-- Larry

thebit

unread,
Jul 19, 2011, 9:50:00 AM7/19/11
to Google Maps JavaScript API v3
I had already seen the example link about reverse-geocoding. In fact I
based my code on this link.
So, you want say to me I cannot to implement this function in my
application because it will not works forever?
Is there a solution?

geoco...@gmail.com

unread,
Jul 19, 2011, 10:10:42 AM7/19/11
to Google Maps JavaScript API v3
On Jul 19, 9:50 am, thebit <the-...@email.it> wrote:
> I had already seen the example link about reverse-geocoding. In fact I
> based my code on this link.
> So, you want say to me I cannot to implement this function in my
> application because it will not works forever?

You can not return the results of an asynchronous query from a
function that originates it.

> Is there a solution?

Do what you need to do in the callback routine.

-- Larry

thebit

unread,
Jul 19, 2011, 10:29:11 AM7/19/11
to Google Maps JavaScript API v3
I am sorry, but I do not understand what to do.
How I can do that by using callback function or callback routine?
Thanks.

thebit

unread,
Jul 27, 2011, 1:39:53 PM7/27/11
to Google Maps JavaScript API v3
Please, can you help me?

Rossko

unread,
Jul 27, 2011, 5:11:54 PM7/27/11
to Google Maps JavaScript API v3
> Please, can you help me?

geocoder.geocode({'latLng': latLng}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
do your jquery stuff here

thebit

unread,
Jul 30, 2011, 7:46:01 AM7/30/11
to Google Maps JavaScript API v3
It's a strange event!
For example, I have the same problem in another function which
converts address -> coordinates
*****************************
// creo la mappa a partire da un indirizzo passato come parametro o
quello di default
function searchAddress(addr)
{
var address;
if(addr == null)
address = document.getElementById("address").value;
else
address = addr;

var geocoder = new google.maps.Geocoder();
geocoder.geocode( {'address': address}, function(results,status)
{
if(status == google.maps.GeocoderStatus.OK)
{
if(addr == null)
map.setCenter(results[0].geometry.location);
else
{
userPosition = results[0].geometry.location;
alert("userPosition = " +userPosition);
}
}
else
{
alert("Error: " +status);
}
});
}

***************************************
In this case I put the code (to store value in a variable) in "(status
== google.maps.GeocoderStatus.OK)" but it does not works!
It seems this function was called after the rest of all the code.
Why?

Rossko

unread,
Jul 30, 2011, 9:20:29 AM7/30/11
to Google Maps JavaScript API v3
> It seems this function was called after the rest of all the code.
> Why?

Because it is asynchronous.
geocoder.geocode( {'address': address}, function( ... ) { ... }
only sends a request to Google. The callback function is NOT run at
this time.
The browser then carries on and executes the code that follows.
For example
geocoder.geocode( {'address': address}, function( ... ) { ... }
alert ('banana');
the alert will display BEFORE the results get back from Google.
Sometime later, the results come back and the callback function will
be run.

In the case of your code snippet, your callback refers to variables
'map' and 'addr' and 'userPosition' that may or may not be within the
scope of the callback function when it runs, you need to be careful
about that.


thebit

unread,
Jul 30, 2011, 12:18:13 PM7/30/11
to Google Maps JavaScript API v3
Well!
Now I know where is the problem, can you give me a little example of
code to store geocode's result in a variable (such as userPosition) ?
I have not good knoweledge about callback function.
Thanks.

Rossko

unread,
Jul 30, 2011, 1:17:18 PM7/30/11
to Google Maps JavaScript API v3
> Now I know where is the problem, can you give me a little example of
> code to store geocode's result in a variable (such as userPosition) ?

It's a useless undertaking ; the rest of your code will not know when
the variable has been populated in the callback function.
Do whatever you want to do with it inside the callback function.
Reply all
Reply to author
Forward
0 new messages