Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return a variable from Google Maps JavaScript geocoder callback?

I am working with the google maps API and whenever I return the variable to the initialize function from the codeLatLng function it claims undefined. If I print the variable from the codeLatLng it shows up fine.

  var geocoder;   function initialize() {     geocoder = new google.maps.Geocoder();     var latlng = new google.maps.LatLng(40.730885,-73.997383);     var addr = codeLatLng();     document.write(addr);    }    function codeLatLng() {     var latlng = new google.maps.LatLng(40.730885,-73.997383);     if (geocoder) {       geocoder.geocode({'latLng': latlng}, function(results, status) {         if (status == google.maps.GeocoderStatus.OK) {           if (results[1]) {                 return results[1].formatted_address;           } else {             alert("No results found");           }         } else {           alert("Geocoder failed due to: " + status);         }       });     }   } 

prints out undefined

If I do:

  var geocoder;   function initialize() {     geocoder = new google.maps.Geocoder();     var latlng = new google.maps.LatLng(40.730885,-73.997383);     codeLatLng();     }    function codeLatLng() {     var latlng = new google.maps.LatLng(40.730885,-73.997383);     if (geocoder) {       geocoder.geocode({'latLng': latlng}, function(results, status) {         if (status == google.maps.GeocoderStatus.OK) {           if (results[1]) {                 document.write(results[1].formatted_address);           } else {             alert("No results found");           }         } else {           alert("Geocoder failed due to: " + status);         }       });     }   } 

prints out New York, NY 10012, USA

like image 544
bmck Avatar asked Jun 07 '10 22:06

bmck


People also ask

What is geocoder in Google Maps?

GET STARTED. Forward Geocoding is the process of converting addresses (like a street address) into geographic coordinates (latitude and longitude), which you can use to place markers on a map or position the map.


1 Answers

You can't return the value from the function, the value doesn't exist yet when the function returns.

The geocode method makes an asynchonous call and uses a callback to handle the result, so you have to do the same in the codeLatLng function:

var geocoder; function initialize() {   geocoder = new google.maps.Geocoder();   var latlng = new google.maps.LatLng(40.730885,-73.997383);   codeLatLng(function(addr){     alert(addr);   }); }  function codeLatLng(callback) {   var latlng = new google.maps.LatLng(40.730885,-73.997383);   if (geocoder) {     geocoder.geocode({'latLng': latlng}, function(results, status) {       if (status == google.maps.GeocoderStatus.OK) {         if (results[1]) {           callback(results[1].formatted_address);         } else {           alert("No results found");         }       } else {         alert("Geocoder failed due to: " + status);       }     });   } } 
like image 194
Guffa Avatar answered Sep 20 '22 02:09

Guffa