reverse geocoding fails with ZERO_RESULTS

738 views
Skip to first unread message

José Luis

unread,
Mar 3, 2010, 6:08:37 PM3/3/10
to Google Maps JavaScript API v3
Hi,

I'm trying to use reverse geocoding to show a popup window with the
clicked address on the map. To this end I have written this code.

var infowindow = new google.maps.InfoWindow();
var geocoder = new google.maps.Geocoder();
google.maps.event.addListener(map, 'click', function(event) {
var location = new google.maps.LatLng(event.latLng);

infowindow.close();
geocoder.geocode({'latLng': location}, function(results,
status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0])

infowindow.setContent(results[0].formatted_address);
} else
alert("Geocoder failed due to: " + status);
});
});

However all I'm getting when trying it on a map is a failure with
ZERO_RESULTS whenever I click on it. Why is it? Have I done something
wrong?

José Luis

unread,
Mar 3, 2010, 8:00:31 PM3/3/10
to Google Maps JavaScript API v3
I found the culprit:

>           var location = new google.maps.LatLng(event.latLng);

>           geocoder.geocode({'latLng': location}, function(results, status) {

event.latLng is already a LatLng, and so the first line is wrong.
event.latLng should be used in place of location.

Reply all
Reply to author
Forward
0 new messages