Strange geocoder behaviour

20 views
Skip to first unread message

Nico

unread,
Jul 20, 2011, 6:45:07 AM7/20/11
to Google Maps JavaScript API v3
Hello,

I noticed some rather strange behaviour concerning the geocoder object
in combination with infoWindows and Markers.

What I want to do: I have two latitude-/longitude values and want to
get the addressinformation with those values. After that I create for
each latitude-/longitude pair a new Marker and add a new attribute
(address) with result[0].formatted_address as value. With those
information I create a new event which displays the adressinformation
in an infoWindow with a click on the specific marker.

Somehow (and don't ask me why) this does only work when I use an alert
in the if(geocoder) function. As soon as I take this alert out it only
displays one of those markers I defined. There are no errors or stuff
like that and unfortunately I don't have enough experience to know why
this is happening. I'd really appreciate it if someone could explain
me what I'm doing wrong.

http://poirecognition.kilu.de/geolocation2.html

Thanks in advance,
Nico

Nico

unread,
Jul 20, 2011, 6:55:35 AM7/20/11
to Google Maps JavaScript API v3
for (i=0; i < latlngArray.length; i++) {
//Distanz zwischen der derzeitigen Position und der Position im
Array wird berechnet
var d =
google.maps.geometry.spherical.computeDistanceBetween(latlng,latlngArray[0]);
//Ist die Distanz geringer als 5km wird ein Marker erstellt
if (d < circle.radius) {
if (geocoder) {
alert(i); // I mean this alert right here
var mp4 = mediaArray[i][0];
var ogv = mediaArray[i][1];
var webm = mediaArray[i][2];
var latlng = latlngArray[i];
var title = titleArray[i];
geocoder.geocode({'latLng': latlng}, function(results,
status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var poiMarker = new google.maps.Marker({
position: latlng,
address: results[0].formatted_address,
map: map,
title: title,
mp4: mp4,
ogv: ogv,
webm: webm
});
info = new google.maps.InfoWindow();
google.maps.event.addListener(poiMarker, 'click',
function(){
//bezieht sich auf die html Eigenschaft des Markers
info.setContent(this.address);
//Bezieht sich auf die Karte und den Marker
info.open(map, this);
});
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
};
};

geoco...@gmail.com

unread,
Jul 20, 2011, 7:05:27 AM7/20/11
to Google Maps JavaScript API v3
How do we get a map to display and exhibit the behavior?

I don't get a map on that page (I declined geolocation).

Geocoding (and reverse geocoding) are asynchronous. If it works with
an alert and doesn't work without it, you don't understand what that
means.

When you call the geocoder, it sends a request to the server and
exits. Sometime later, the request comes back from the server and the
callback function runs.

-- Larry

-- Larry

>
> Thanks in advance,
> Nico

Nico

unread,
Jul 20, 2011, 7:21:25 AM7/20/11
to Google Maps JavaScript API v3
And why is the second marker displayed with the correct information
then? Why does the correct information appear when I use alert with
the result-object? Why does this only happen when I use alert in the
if (geocoder) function and nowhere else? Why doesn't it work with
something random, like creating a new variable?

Even if you are correct and this has something to do with geocoding
being asynchronous I still don't know how to solve this problem
without using alert. I already said that im pretty inexperienced...

I'll take the part out where the document asks for your position ...
as I'm not really using it anyways.

Nico

unread,
Jul 20, 2011, 8:21:07 AM7/20/11
to Google Maps JavaScript API v3
Ok .. I tried a few things and it seems that I don't quite understand
the context of asynchronous here...

I noticed that the reversegeocoding only works when I insert the index
of my for-loop into alert. So I thought that I basically have to spend
as much time as getting the index and made some kind of wait-function
and delayed the program for up to 1 second and at the same point as
the alert-function, but it didn't work. You might call this attempt
stupid, but I don't know what else to do.

I'm really trying hard to understand what's going on and how to find
some kind of solution, but I just can't do it on my own.

Armand Lems

unread,
Jul 20, 2011, 9:05:55 AM7/20/11
to Google Maps JavaScript API v3
Hi,

since the request is async, that implies that the classic way of
programming where everything is sequential doesn't work :
Exemple : if you do your geocode request on line '1' - stepping to
line '2' doesn't mean that you have received the answer from the
geocoder yet
That's why you have a callback function : "function(results,status)
{ };" which can be called at anytime, when your answer has been
camputed by google

So you can imbricate the second geocoder call in the first callback
function
or trig an event yourself after each receive to start the next
geocoder call

geoco...@gmail.com

unread,
Jul 20, 2011, 9:08:51 AM7/20/11
to Google Maps JavaScript API v3
See if this does what you want and makes sense to you:
http://www.geocodezip.com/poirecognition_kilu_de_geolocation2a.html

-- Larry

MymsMan

unread,
Jul 20, 2011, 9:53:26 AM7/20/11
to google-map...@googlegroups.com
Nico,

I'm new to this as well but you need to put ALL of the processing of setting your marker and infowindow in the call back routine after 'if (results[0])' so that it is not executed until your address has been geocoded successfully.

Using Alert delays the process sufficiently so that the code outside the callback doesn't get called until the callback has had time to finish. JavaScript doesn't have a wait function so you cant easily wait in your main routine for the asynchronous routine to complete. 

geoco...@gmail.com

unread,
Jul 20, 2011, 5:43:43 PM7/20/11
to Google Maps JavaScript API v3
On Jul 20, 9:08 am, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
A couple of other points:
1. This approach will only work for up to ~ 10 markers (depending on
the current rate limit and quota on the service)
2. This is not the "right" way to do it. You should reverse geocode
the points when you capture them and store them in your database,
rather than doing it every time your map loads (unless they will be
different every time your map loads).

  -- Larry
Reply all
Reply to author
Forward
0 new messages