TypeError:对象没有方法'split'拆分结果[0] .geometry.location时出错

时间:2011-11-14 18:59:55

标签: javascript google-maps-api-3

拆分results[0].geometry.location时出错。我想分裂这个

geocoder = new google.maps.Geocoder();
codeAddress();

function codeAddress() {
    var address = "Karachi, Pakistan";
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var variable = results[0].geometry.location;
           // this will return latitutde and longitutde, I want to split this
           // because it is in a format like (54.8773,99.8038994749)
           var next = variable.split(",");
           // this giving an error "TypeError: Object has no method 'split'"
        }
    });
}

1 个答案:

答案 0 :(得分:5)

location propertyLatLng个实例,而不是数组。使用lat()lng()方法提取坐标:

var variable = results[0].geometry.location;
var next     = [ variable.lat(), variable.lng() ];