Google地理编码器未正确返回值

时间:2014-08-04 09:55:40

标签: javascript google-maps google-geocoder

我正在使用谷歌地理编码器来获取与searchterm相关的坐标。

我的代码如下:

var geocoder = new google.maps.Geocoder();
var location = codeAddress(places[index].name, geocoder);
var res = location.split(";");

function codeAddress(sAddress, geocoder) {
    var address = sAddress;
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK){
            return  results[0].geometry.location.lat() + ';' +            results[0].geometry.location.lng();
        }else{
            return null;
        }
    });
}

但我得到了

Uncaught TypeError: Cannot read property 'split' of undefined
位于location.split(“;”);

中的

如果我打印代码地址,如果阻止

console.log("latitude: " + results[0].geometry.location.lat());
console.log("longitude: " + results[0].geometry.location.lng());

我得到正确的值,但是如果我打印位置它返回undefined。

0 个答案:

没有答案