geoCoder 响应循环 - 无法读取未定义的 属性“0”
geoCoder respsone loop - Cannot read property '0' of undefined
我正在向 google 地理编码器发送 lon/lat 并获取试图分解为变量的响应数据,响应如下所示:
这是我的代码:
getFormattedAddress(result) {
const arrResult = result.address_components;
let itemRoute = '';
let itemLocality = '';
let itemCountry = '';
let itemPc = '';
let itemSnumber = '';
for(let address_item in arrResult) {
if (address_item.types[0] == "route") {
console.log(i + ": route:" + address_item.long_name);
itemRoute = address_item.long_name;
}
if (address_item.types[0] == "locality") {
console.log("town:" + address_item.long_name);
itemLocality = address_item.long_name;
}
if (address_item.types[0] == "country") {
console.log("country:" + address_item.long_name);
itemCountry = address_item.long_name;
}
if (address_item.types[0] == "postal_code_prefix") {
console.log("pc:" + address_item.long_name);
itemPc = address_item.long_name;
}
if (address_item.types[0] == "street_number") {
console.log("street_number:" + address_item.long_name);
itemSnumber = address_item.long_name;
}
}
}
我得到的错误是:
未捕获(承诺)TypeError:无法读取未定义的 属性“0”
我错过了什么?
更新,json输出:
[
{
"long_name": "29-31",
"short_name": "29-31",
"types": [
"street_number"
]
},
{
"long_name": "Götgatan",
"short_name": "Götgatan",
"types": [
"route"
]
},
{
"long_name": "Södermalm",
"short_name": "Södermalm",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Stockholm",
"short_name": "Stockholm",
"types": [
"locality",
"political"
]
},
{
"long_name": "Stockholm",
"short_name": "Stockholm",
"types": [
"postal_town"
]
},
{
"long_name": "Stockholms län",
"short_name": "Stockholms län",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Sverige",
"short_name": "SE",
"types": [
"country",
"political"
]
},
{
"long_name": "118 26",
"short_name": "118 26",
"types": [
"postal_code"
]
}
]
var arr = [
{
"long_name": "29-31", "short_name": "29-31",
"types": [ "street_number" ]
},
{
"long_name": "Götgatan", "short_name": "Götgatan",
"types": [ "route" ]
},
{
"long_name": "Södermalm", "short_name": "Södermalm",
"types": [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name": "Stockholm", "short_name": "Stockholm",
"types": [ "locality", "political" ]
},
{
"long_name": "Stockholm", "short_name": "Stockholm",
"types": [ "postal_town" ]
},
{
"long_name": "Stockholms län", "short_name": "Stockholms län",
"types": [ "administrative_area_level_1", "political" ]
},
{
"long_name": "Sverige", "short_name": "SE",
"types": [ "country", "political" ]
},
{
"long_name": "118 26", "short_name": "118 26",
"types": [ "postal_code" ]
}
]
for (var i=0; i< arr.length ; i++) {
if (arr[i].types.indexOf('route') > -1)
console.log( 'route ' + arr[i].long_name );
if (arr[i].types.indexOf('locality') > -1)
console.log( 'locality ' + arr[i].long_name );
}
我正在向 google 地理编码器发送 lon/lat 并获取试图分解为变量的响应数据,响应如下所示:
这是我的代码:
getFormattedAddress(result) {
const arrResult = result.address_components;
let itemRoute = '';
let itemLocality = '';
let itemCountry = '';
let itemPc = '';
let itemSnumber = '';
for(let address_item in arrResult) {
if (address_item.types[0] == "route") {
console.log(i + ": route:" + address_item.long_name);
itemRoute = address_item.long_name;
}
if (address_item.types[0] == "locality") {
console.log("town:" + address_item.long_name);
itemLocality = address_item.long_name;
}
if (address_item.types[0] == "country") {
console.log("country:" + address_item.long_name);
itemCountry = address_item.long_name;
}
if (address_item.types[0] == "postal_code_prefix") {
console.log("pc:" + address_item.long_name);
itemPc = address_item.long_name;
}
if (address_item.types[0] == "street_number") {
console.log("street_number:" + address_item.long_name);
itemSnumber = address_item.long_name;
}
}
}
我得到的错误是: 未捕获(承诺)TypeError:无法读取未定义的 属性“0”
我错过了什么?
更新,json输出:
[
{
"long_name": "29-31",
"short_name": "29-31",
"types": [
"street_number"
]
},
{
"long_name": "Götgatan",
"short_name": "Götgatan",
"types": [
"route"
]
},
{
"long_name": "Södermalm",
"short_name": "Södermalm",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Stockholm",
"short_name": "Stockholm",
"types": [
"locality",
"political"
]
},
{
"long_name": "Stockholm",
"short_name": "Stockholm",
"types": [
"postal_town"
]
},
{
"long_name": "Stockholms län",
"short_name": "Stockholms län",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Sverige",
"short_name": "SE",
"types": [
"country",
"political"
]
},
{
"long_name": "118 26",
"short_name": "118 26",
"types": [
"postal_code"
]
}
]
var arr = [
{
"long_name": "29-31", "short_name": "29-31",
"types": [ "street_number" ]
},
{
"long_name": "Götgatan", "short_name": "Götgatan",
"types": [ "route" ]
},
{
"long_name": "Södermalm", "short_name": "Södermalm",
"types": [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name": "Stockholm", "short_name": "Stockholm",
"types": [ "locality", "political" ]
},
{
"long_name": "Stockholm", "short_name": "Stockholm",
"types": [ "postal_town" ]
},
{
"long_name": "Stockholms län", "short_name": "Stockholms län",
"types": [ "administrative_area_level_1", "political" ]
},
{
"long_name": "Sverige", "short_name": "SE",
"types": [ "country", "political" ]
},
{
"long_name": "118 26", "short_name": "118 26",
"types": [ "postal_code" ]
}
]
for (var i=0; i< arr.length ; i++) {
if (arr[i].types.indexOf('route') > -1)
console.log( 'route ' + arr[i].long_name );
if (arr[i].types.indexOf('locality') > -1)
console.log( 'locality ' + arr[i].long_name );
}