3 回答
TA贡献1820条经验 获得超10个赞
有点晚了,但如果它对任何人都有帮助,可以这样做:
如果您只需要第一个结果
function handleGooglePlacesPress = results => {
const map = {
street_number: 'streetNumber',
route: 'streetName',
sublocality_level_1: 'city',
locality: 'city1',
administrative_area_level_1: 'state',
postal_code: 'postalCode',
country: 'country'
};
const brokenDownAddress = {};
results[0].address_components.forEach(component => {
brokenDownAddress[map[component.types[0]]] = component.long_name;
}
return brokenDownAddress;
}
TA贡献1875条经验 获得超5个赞
您可以使用 for 循环并检查types值,这是一个简单的示例:
var add_comps = json['result']['address_components'];
for (var i = 0; i < add_comps.length; i++) {
if (add_comps[i]['types'] == 'street_number') {
console.log(i);
}
}
TA贡献1873条经验 获得超9个赞
好的,所以我最终只是循环通过 details.address_components 来分配变量。我检查了两次城市,因为有时它被称为“locality”,有时它被称为“subLocality_level_1”,具体取决于我相信的国家:
function handleGooglePlacesPress(data,details){
var StreetNumber = null;
var StreetName = null;
var City = null;
var State = null;
var Country = null;
var PostalCode = null;
const addressComponents = details.address_components;
addressComponents.forEach(addressComponent => {
const addressType = addressComponent.types[0]
if(addressType == 'street_number'){
StreetNumber = addressComponent.long_name;
}
if(addressType == 'route'){
StreetName = addressComponent.long_name;
}
if(addressType == 'sublocality_level_1'){
City = addressComponent.long_name;
}
if(addressType == 'locality'){
City = addressComponent.long_name;
}
if(addressType == 'administrative_area_level_1'){
State = addressComponent.long_name
}
if(addressType == 'postal_code'){
PostalCode = addressComponent.long_name
}
if(addressType == 'country'){
Country = addressComponent.long_name
}
});
console.log(StreetNumber,StreetName,City,State,PostalCode,Country) //logs each address piece
添加回答
举报