-1

I use this function to get lat/long from google maps:

function getLatLong(address){
  var geo = new google.maps.Geocoder;

  geo.geocode({'address':address},function(results, status){
    if (status == google.maps.GeocoderStatus.OK) {
      return (results[0].geometry.location.k + ',' + results[0].geometry.location.D);
    } else {
      return;
    }

  });
}

and it kind of does what I want it to do, but when I use it within another function like so: getLatLong('new york, times square'), I get undefined in return. If I set console.log(results[0].geometry.location.k + ',' + results[0].geometry.location.D); and not return, I get the location in console (if I call the function on, for example, document.ready).

Why is that and how should I use it correctly?

Xeen
  • 6,955
  • 16
  • 60
  • 111

2 Answers2

1

The joke in here is that geoCode does not return anything, but it expects you to handle the map showing inside the function callback directly. You can right put it in console.log and do whatever, but not return and process out of the function.

Look for example https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple and you'll see how this is true.

Edit:

Some workaround exists still, look at this code:

 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); 
                   } 
               }); 
          } 
     }

The above code snippet tells how you can use explicit callback.

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

Edit2:

Your code modified accordingly:

  function getLatLong(address, callback){ 
      var geo = new google.maps.Geocoder; 
      geo.geocode({'address':address,   function(results, status){ 
           if (status == google.maps.GeocoderStatus.OK) { 
                 callback (results[0].geometry.location.k + ',' + results[0].geometry.location.D); 
            } else { 
                 // do sth with error. 
            } 
        }); 
   }

and calling it:

 getLatLong(address, function(addr){ 
          // do whatever with addr attribute
          alert(addr); 
       }); 

Maybe that helps you get an idea.

Community
  • 1
  • 1
mico
  • 12,730
  • 12
  • 59
  • 99
  • But surely there has to be some kind of a solution, no? – Xeen Apr 09 '15 at 06:43
  • yes, here: http://stackoverflow.com/questions/2993563/how-do-i-return-a-variable-from-google-maps-javascript-geocoder-callback – mico Apr 09 '15 at 06:49
  • Updated answer with the main point of the mentioned post. – mico Apr 09 '15 at 07:24
  • Um.. can you please show me how to implement the necessarry part in my code? I kind of don't get it I think.. – Xeen Apr 09 '15 at 08:22
  • look my latest edit. – mico Apr 09 '15 at 08:48
  • Thanks! But this kind of leaves me where I started because I can display that content through `console.log` and `alert`, but not through `return`. That is `var myLatLng = new google.maps.LatLng(getLatLong("= $r['address'] ?>", function(addr){ return addr; }));` still doesn't receive the lat/long values :( – Xeen Apr 09 '15 at 09:00
0

You are calling getLantLong('new york, times square');

instead of getLatLong('new york, times square');

Check your spelling

Ethaan
  • 11,291
  • 5
  • 35
  • 45
kabangi julius
  • 2,709
  • 2
  • 16
  • 25