谷歌地图地理编码服务
我正在使用谷歌地图 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;
}
另外我如何访问邮政编码?
place.AddressDetails.AdministrativeArea.SubAdministrativeArea.PostalCode.PostalCodeNumber
I am using google maps API geocoding service to get location's country name, postal code etc.
JSON response:
{
"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]
}
}
]
}
Here is the problem - sometimes (despite checking for undefined) I get an error that country is undefined.
var country = document.getElementById("id_country");
if(place.AddressDetails.Country.CountryNameCode != undefined){
country.value = place.AddressDetails.Country.CountryNameCode;
}
Also how can I access PostalCodeNumber?
place.AddressDetails.AdministrativeArea.SubAdministrativeArea.PostalCode.PostalCodeNumber
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大多数时候,当我尝试这样的代码时:
我没有得到任何东西,我不知道如何解释它,但我的队友编写了一个脚本来手动解析它,并且它对我们来说工作得很好(虽然代码很多)。您可以尝试并编辑它以获取其余缺失字段(您的邮政编码):
Most of the time when I try code such as:
I dont get anything and I have no idea how to explain it, but my team mate wrote a script to manually parse it and it works fine for us (code is alot though). You can try it and edit it for getting the rest of missing fields (your postal code):