1
我使用谷歌地圖API地址解析服務來獲得位置的國家名稱,郵政編碼等谷歌地圖的地理編碼服務
JSON響應:
{
"name": "1600 Amphitheatre Parkway, Mountain View, CA, USA",
"Status": {
"code": 200,
"request": "geocode"
},
"Placemark": [
{
"address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"AddressDetails": {
"Country": {
"CountryNameCode": "US",
"AdministrativeArea": {
"AdministrativeAreaName": "CA",
"SubAdministrativeArea": {
"SubAdministrativeAreaName": "Santa Clara",
"Locality": {
"LocalityName": "Mountain View",
"Thoroughfare": {
"ThoroughfareName": "1600 Amphitheatre Pkwy"
},
"PostalCode": {
"PostalCodeNumber": "94043"
}
}
}
}
},
"Accuracy": 8
},
"Point": {
"coordinates": [-122.083739, 37.423021, 0]
}
}
]
}
這裏的問題是 - 有時(儘管檢查未定義)我得到一個國家未定義的錯誤。
var country = document.getElementById("id_country");
if(place.AddressDetails.Country.CountryNameCode != undefined){
country.value = place.AddressDetails.Country.CountryNameCode;
}
另外如何訪問PostalCodeNumber?
place.AddressDetails.AdministrativeArea.SubAdministrativeArea.PostalCode.PostalCodeNumber