作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用谷歌地图 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
最佳答案
大多数时候,当我尝试以下代码时:
place.AddressDetails.AdministrativeArea. //etc.
我没有得到任何东西,我不知道如何解释它,但是我的队友编写了一个脚本来手动解析它,并且它对我们来说工作得很好(虽然代码很多)。您可以尝试并编辑它以获取其余缺失字段(您的邮政编码):
//[MARKEL]: Returns object type: obj.ad1, obj.ad2, obj.ad3, obj.state, obj.country
function ResolveGeoCode(point, returnCall) {
//[MARKEL]: initialize geocoder
geocoder = new GClientGeocoder();
geocoder.getLocations(point, function getAddress(response) {
//[MARKEL]: Create a object to call proxy location Set location variable to be global
//because it will be assigned in call-back function
var Location;
if (!response || response.Status.code != 200) {
//MARKEL: [TODO] => Set code here to alert that the address id invalid
}
else {
place = response.Placemark[0];
place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1],
place.Point.coordinates[0]);
var len = place.address.toString().split(",").length;
var array = place.address.toString().split(",");
//alert(place.address);
//alert(len);
if (len >= 3) {
if (array[0].length > 2) {
Location = {
Street: array[0],
State: array[1],
Country: array[2]
};
}
else {
Location = {
Street: array[1],
State: "",
Country: array[2]
};
}
}
else if (len == 2) {
Location = {
Street: "",
State: array[0],
Country: array[1]
};
}
else if (len == 1) {
Location = {
Sreet: "",
State: "",
Country: array[0]
};
}
else {
//[MARKEL]: [TODO] => Place code here to indicate that the address is not valid.
}
}
returnCall(Location);
return Location;
});
}
关于javascript - 谷歌地图地理编码服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2350240/
leaflet:一个开源并且对移动端友好的交互式地图 JavaScript 库 中文文档: https://leafletjs.cn/reference.html 官网(英文): ht
我是一名优秀的程序员,十分优秀!