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