I am using Google maps v3 geocoder to geocode an address then pass the 2 coordinate points from the jQuery file to a PHP file using getJSON
.
Problem: However, I notice that the function that does the geocoding function keeps returning an undefined value! Thus the PHP file receives a undefined variable. Where did I go wrong?
jQuery Code
var search_latlng = geocodeAddress(search_location);
console.log(search_latlng);
$.getJSON('/main/get_places', {search_location: search_latlng}, function(json){
$("#result_listing").html('');
.
.
.
Geocoder JS function
function geocodeAddress(address) {
var latlng = new Array(2);
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latlng[0] = results[0].geometry.location.lat();
latlng[1] = results[0].geometry.location.lng();
return latlng;
} else {
console.log("Geocode was not successful for the following reason: " + status);
}
});
}
See Question&Answers more detail:os