Using a callback function with Google Geocode

后端 未结 1 1525
终归单人心
终归单人心 2020-12-18 08:52

I\'ve been struggling with this for a few hours now, and even after reading several examples on Stack I\'ve been unable to get this working. It doesn\'t help that I\'m a JS

相关标签:
1条回答
  • 2020-12-18 09:34

    Your problem is, that you didn't connect the callback to the return. As the geocode() function itself is already asynchronous, the return doesn't have any effect there. Instead you have to pass the values you are returning here directly to the callback-function. Like this:

    function getLocationData(position, callback) {
      geocoder = new google.maps.Geocoder();
      var location = 'Billings,MT';
    
      if( geocoder ) {
        geocoder.geocode({ 'address': location }, function (results, status) {
          if( status == google.maps.GeocoderStatus.OK ) {
            callback(results[0]);
          }
        });
      }
    }
    
    0 讨论(0)
提交回复
热议问题