I am using Google Maps V3 API and using its Geocoder to do reverse geocoding of the point on the map where the user clicks on.
Load the geocoding function when DOM is fully loaded
$(function() {
reverse_geocode(40.714224,-73.961452);
});
Function that does the reverse geocoding:
function reverse_geocode(lat,lng) {
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(10,-10);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert('asdasd');
} else {
alert("Geocoder failed due to: " + status);
}
});
}
If I call the reverse_geocode function when the page first loads, everything works fine and the alert() is called.
However, if I call reverse_geocode() only when triggered by a rightclick on the map followed by a mouseclick on a div, using the code below, nothing happens! Any idea what went wrong?
google.maps.event.addListener(map, "rightclick", function(event) {
//some code not shown
$("#info_rightclick_top").click(function(e) {
info_ok_handleclick();
});
});
function info_ok_handleclick() {
$("#info_ok").click(function(e) {
var lat = marker_search_location.getPosition().lat();
var lng = marker_search_location.getPosition().lng();
var latlng = lat + "_" + lng;
reverse_geocode(10,-10);
// some code hidden
});
Using this bellow code you can search a location by keywords.