Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javascript函数返回地址位置_Javascript_Google Maps Api 3_Return Value - Fatal编程技术网

从javascript函数返回地址位置

从javascript函数返回地址位置,javascript,google-maps-api-3,return-value,Javascript,Google Maps Api 3,Return Value,我正在使用google map api V3,希望通过使用下一个函数获取某个地址的位置: function findAddress(address) { var geocoder = new google.maps.Geocoder(); var addrLocation = ""; geocoder.geocode( { 'address': address}, function(results, status) { if (status == google

我正在使用google map api V3,希望通过使用下一个函数获取某个地址的位置:

function findAddress(address) {

    var geocoder = new google.maps.Geocoder();
    var addrLocation = "";

    geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        addrLocation = "Location is: " + results[0].geometry.location;
    } else {
        addrLocation = "Not found"; }   
    });

    return addrLocation;
}
function SomeFunction(){
  alert(findAddress("New York"));
}
然后我尝试调用findAddress函数:

function findAddress(address) {

    var geocoder = new google.maps.Geocoder();
    var addrLocation = "";

    geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        addrLocation = "Location is: " + results[0].geometry.location;
    } else {
        addrLocation = "Not found"; }   
    });

    return addrLocation;
}
function SomeFunction(){
  alert(findAddress("New York"));
}

按照我的逻辑,该警报应该返回'addrLocation'值,但它会返回空白消息。我做错了什么?

您不能从异步调用返回一个值,因为这是每小时询问一次的,所以要查找一个dupe

您不能从异步调用返回值,因为这是每小时询问一次的,所以要查找一个dupe

可能重复的谢谢!我也发现了同样的问题。从异步调用返回值是不可能的,但我可以将这些值存储在全局变量中。谢谢大家。非常感谢可能的副本!我也发现了同样的问题。从异步调用返回值是不可能的,但我可以将这些值存储在全局变量中。谢谢大家。