简体   繁体   English

从javascript函数获取Google api地址值作为返回值

[英]get google api address value as return from javascript function

  • I am trying to get the address from the google maps api and return this address in the function.What do I need to change to make these show up? 我正在尝试从google maps api获取地址并在函数中返回该地址。我需要进行哪些更改才能显示这些地址?

$(document).ready(function() {
  var lat = 17.4807865;
  var lng = 78.4921649;
  var returnAddress = getAddressFromlatlng(function(backaddr,lat,lng){
     alert(" Inside : "+backaddr)
  });
  alert(returnAddress+" returned address ")
});
function getAddressFromlatlng(callback,lat,lng){
  alert(lat+" : "+lng)
  var geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(lat,lng);
  geocoder.geocode({'latLng': latlng}, function(results, status){   
    alert(results[0].formatted_address)
    callback(results[0].formatted_address);
  });
}

That's not possible, as the function returns before the value exists. 这是不可能的,因为函数会在值存在之前返回。

The geocode call is asynchronous, so you have to handle the result asynchronously. geocode调用是异步的,因此您必须异步处理结果。 You already display the value successfully in the callback function, that is the way to handle the result. 您已经在回调函数中成功显示了该值,这是处理结果的方法。

You could return a promise from the getAddressFromlatlng function, but that doesn't really change anything. 您可以从getAddressFromlatlng函数返回一个getAddressFromlatlng ,但这并没有真正改变任何东西。 That's just wrapping the callback function in an object so that you can do the asynchronous handling of the result separate from the call. 那只是将回调函数包装在一个对象中,以便您可以将结果与调用分开进行异步处理。

You should be taking the address from the result of the callback. 您应该从回调结果中获取地址。 Once the coordinates have been resolved you call a function that continues processing the returned value because only then do you have the return value. 坐标解析后,您将调用一个继续处理返回值的函数,因为只有这样您才具有返回值。 In the case your going through an array use the array index and update the address field once you have the required data. 如果您要遍历数组,则在拥有所需数据后使用数组索引并更新地址字段。 or process them individualy. 或单独处理它们。 work through the array making a request to google for every item. 遍历数组,向每个项目向Google发出请求。 once the response comes back from google, move onto the next item in your array. 响应从Google返回后,移至数组中的下一项。 If you can save the index so its known when the response arrives then it would be possible to wait for multiple requests. 如果您可以保存索引,以便在响应到达时知道该索引,则可以等待多个请求。

var currentIndex = null;
function notifyAddressResolved (address) {
    yourArray[currentIndex].address = address;
    resolveNextAddress();
}
function getAddressFromlatlng(callback, lat, lng){
    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(lat, lng);  
    geocoder.geocode({'latLng': latlng}, function (results, status) {           
        callback(results[0].formatted_address);
    });
}
function resolveNextAddress () {
    if (currentIndex == null) {
        currentIndex = 0;
    } else {
        currentIndex++;
    }
    getAddressFromlatlng(notifyAddressResolved, yourArray[currentIndex].lat, yourArray[currentIndex].lng);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM