如何使用反向地理编码获取 Mapbox 的城市/州/国家/地区?
How to get City / State / Country for Mapbox using reverse geocoding?
我读过这个 API 并根据一些示例测试了很多坐标
https://www.mapbox.com/developers/api/geocoding/
是一个提供了大量信息的示例,问题是没有简单的方法来获取我能够找到的城市、州和国家/地区。
我可以得到这样的结果
Mc Lean, 22102, 弗吉尼亚州, 美国
旧金山,94103,加利福尼亚州,美国
英国伦敦,威斯敏斯特市 SW1H6
旧金山,94114,加利福尼亚州,美国
珀斯, 6053, 西澳大利亚州, 澳大利亚
问题是我必须弄清楚如何解析每个国家/地区的邮政编码(不合理)。
我觉得http://api.tiles.mapbox.com/v4/geocode/mapbox.places/ might be the wrong endpoint. I found http://api.tiles.mapbox.com/v4/geocode/mapbox.places-country-v1/在某一时刻,但它只会给国家名称。
关于如何从 Mapbox 反向地理编码 api 查找中获取 city/state/country 数据的任何指导?
我最终根据 mapbox 返回的内容想出了这个函数。根节点给出了town/city,然后给出了它周围的上下文。这是在重建字符串时检查哪个上下文是邮政编码并将其排除在外的问题。
//builds proper format of location string based on mapbox data. city,state/province,country
function parseReverseGeo(geoData) {
// debugger;
var region, countryName, placeName, returnStr;
if(geoData.context){
$.each(geoData.context, function(i, v){
if(v.id.indexOf('region') >= 0) {
region = v.text;
}
if(v.id.indexOf('country') >= 0) {
countryName = v.text;
}
});
}
if(region && countryName) {
returnStr = region + ", " + countryName;
} else {
returnStr = geoData.place_name;
}
return returnStr;
}
同上。当我使用地理编码器结果数据设置输入字段时,我将使用这种格式。
placeName = geoData.place_name.split(',');
$('#addressLine1').val(placeName[1]);
if(v.id.indexOf('region') >= 0) {
region = v.text;
$('input[name="vstate"]').val(region);
}
if(v.id.indexOf('postcode') >= 0) {
postcode = v.text;
$('input[name="vpostalCode"]').val(postcode);
}
if(v.id.indexOf('place') >= 0) {
city = v.text;
$('input[name="vcity"]').val(city);
}
if(v.id.indexOf('country') >= 0) {
countryName = v.text;
if (countryName.indexOf('United States') > 0) {
$('input[name="vcountry"]').val("US");
}
if (countryName.indexOf('Canada') > 0) {
$('input[name="vcountry"]').val("CA");
}
else {
$('input[name="vcountry"]').val("US");
}
}
我读过这个 API 并根据一些示例测试了很多坐标 https://www.mapbox.com/developers/api/geocoding/
是一个提供了大量信息的示例,问题是没有简单的方法来获取我能够找到的城市、州和国家/地区。
我可以得到这样的结果
Mc Lean, 22102, 弗吉尼亚州, 美国
旧金山,94103,加利福尼亚州,美国
英国伦敦,威斯敏斯特市 SW1H6
旧金山,94114,加利福尼亚州,美国
珀斯, 6053, 西澳大利亚州, 澳大利亚
问题是我必须弄清楚如何解析每个国家/地区的邮政编码(不合理)。
我觉得http://api.tiles.mapbox.com/v4/geocode/mapbox.places/ might be the wrong endpoint. I found http://api.tiles.mapbox.com/v4/geocode/mapbox.places-country-v1/在某一时刻,但它只会给国家名称。
关于如何从 Mapbox 反向地理编码 api 查找中获取 city/state/country 数据的任何指导?
我最终根据 mapbox 返回的内容想出了这个函数。根节点给出了town/city,然后给出了它周围的上下文。这是在重建字符串时检查哪个上下文是邮政编码并将其排除在外的问题。
//builds proper format of location string based on mapbox data. city,state/province,country
function parseReverseGeo(geoData) {
// debugger;
var region, countryName, placeName, returnStr;
if(geoData.context){
$.each(geoData.context, function(i, v){
if(v.id.indexOf('region') >= 0) {
region = v.text;
}
if(v.id.indexOf('country') >= 0) {
countryName = v.text;
}
});
}
if(region && countryName) {
returnStr = region + ", " + countryName;
} else {
returnStr = geoData.place_name;
}
return returnStr;
}
同上。当我使用地理编码器结果数据设置输入字段时,我将使用这种格式。
placeName = geoData.place_name.split(',');
$('#addressLine1').val(placeName[1]);
if(v.id.indexOf('region') >= 0) {
region = v.text;
$('input[name="vstate"]').val(region);
}
if(v.id.indexOf('postcode') >= 0) {
postcode = v.text;
$('input[name="vpostalCode"]').val(postcode);
}
if(v.id.indexOf('place') >= 0) {
city = v.text;
$('input[name="vcity"]').val(city);
}
if(v.id.indexOf('country') >= 0) {
countryName = v.text;
if (countryName.indexOf('United States') > 0) {
$('input[name="vcountry"]').val("US");
}
if (countryName.indexOf('Canada') > 0) {
$('input[name="vcountry"]').val("CA");
}
else {
$('input[name="vcountry"]').val("US");
}
}