Javascript函数没有从另一个函数内部返回值

时间:2014-10-24 20:25:54

标签: javascript function

有人可以告诉我为什么thelocation在此示例中返回为未定义?

function codeAddress(business,address,i,locations) {
    var thelocation = geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var loc = [business, results[0].geometry.location.lat(), results[0].geometry.location.lng(), i];
            return loc;
        } 
    });
    console.log(thelocation);
}

非常感谢你的帮助。

修改

这是完整的上下文...我有一些来自数据库的值作为lat&但只是其他人的地址。我必须填充我的数组以创建带有标记的地图,但其中一些必须通过地理编码器。这就是我所拥有的显然不起作用的东西:

var locations = [];

geocoder = new google.maps.Geocoder();

<?php
    if ( !empty($posts) ) {
        $i = 1;
        foreach ( $posts as $mPost ) {

            //If we find a lat/lng override value
            if ( get_field('lat_lng_override', $mPost->ID) ) {
                $latlng = explode(",",get_field('lat_lng_override', $mPost->ID));
                echo "locations[{$i}] = ['". addslashes(str_replace("&","&amp;",$mPost->post_title))."', ".$latlng[0].", ".$latlng[1].", ".$i."];";
            } else {
            //Can't find lat/lng so get it from google
                echo "codeAddress('".addslashes(str_replace("&","&amp;",$mPost->post_title))."','".get_field('business_address', $mPost->ID).", ".get_field('city', $mPost->ID).", ON',".$i.",locations, function(loc){ locations.push(loc); });";
            }

            $i++;
        }
    }
?>

console.log(locations);

function codeAddress(business,address,i,locations, callback) {
    var thelocation = geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var loc = [business, results[0].geometry.location.lat(), results[0].geometry.location.lng(), i];
        callback(loc);
      } 
    });
}

我的想法是,当我完成时,我的数组看起来与此相似:

var locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

提前致谢。

2 个答案:

答案 0 :(得分:1)

因为.geocode是异步函数,所以你需要使用回调!

function codeAddress(business,address,i,locations, callback) {
    var thelocation = geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var loc = [business, results[0].geometry.location.lat(), results[0].geometry.location.lng(), i];
            callback(loc);
        } 
    });
    console.log(thelocation);
}

然后使用它:

codeAddress(business, address, i, locations, function(location) {
    console.log(location);
});

答案 1 :(得分:1)

从理论上讲,你无法捕捉到asynctask之外的位置值,因为你只是不知道它什么时候会完成。所以你必须在你的回调中捕获它,所以,

function codeAddress(business,address,i,locations) {
var thelocation = geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        var loc = [business, results[0].geometry.location.lat(), results[0].geometry.location.lng(), i];
        //return location;
 console.log(loc);
 //continue with what you would like to do with the results, something small i assume! :)
    } 
  });

}