Javascript函数返回时未定义

时间:2015-12-28 17:13:01

标签: javascript node.js scope

当我运行函数getCoordinates和console.log我想要的两个变量时,该函数按计划工作并在控制台中显示:30.4190671 -97.74594990000003。这是wowrking代码...

getCoordinates("5401 Duval Austin TX")

function getCoordinates (address) {
    geocoder = new google.maps.Geocoder();
    var coordinatesA;
    var coordinatesB;
    geocoder.geocode({ address: address}, function (results, status) {
    coordinatesA = results[0].geometry.location.lat()
    coordinatesB = results[0].geometry.location.lng()
    console.log(coordinatesA, coordinatesB)
    })
};

但是,当我在函数末尾添加return行以便处理数据时。我console.log(getCoordinates("5401 Duval Austin TX")) 我得到undefined这里是完整的代码块:

console.log(getCoordinates("5401 Duval Austin TX"))

function getCoordinates (address) {
    geocoder = new google.maps.Geocoder();
    var coordinatesA;
    var coordinatesB;
    geocoder.geocode({ address: address}, function (results, status) {
    coordinatesA = results[0].geometry.location.lat()
    coordinatesB = results[0].geometry.location.lng()
    return coordinatesA + coordinatesB
    })
};

是什么给出了?

0 个答案:

没有答案