如何从 Google 地理编码 API 中仅获取 administrative_area_level_2?
How to get only administrative_area_level_2 from Google Geocoding API?
我试图从 Google 地理定位 API 结果中仅获取县名。不幸的是,结果并不总是在同一个地方,所以我需要通过一个识别键来获取它 administrative_area_level_2
这是我的电话:
$.getJSON( {
url : 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + ',' + position.coords.longitude + '&sensor=true',
data : {
sensor : false
},
success : function( data, textStatus ) {
var county = data.results[0].address_components[4].long_name;
alert('County: ' + county);
}
});
这适用于我所在的县,但不适用于其他县,因为 address_components[4]
并不总是该县。但是,我确实注意到该县始终与 administrative_area_level_2
相关联
这是一个示例响应:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Edge of Everglades Trail",
"short_name" : "Edge of Everglades Trail",
"types" : [ "route" ]
},
{
"long_name" : "Homestead",
"short_name" : "Homestead",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Miami-Dade County",
"short_name" : "Miami-Dade County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Florida",
"short_name" : "FL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "33031",
"short_name" : "33031",
"types" : [ "postal_code" ]
}
],
...
我将如何获得与 administrative_area_level_2
类型关联的 "long_name"?
为了从地址组件数组中获取 administrative_area_level_2
,您可以使用 Array.prototype.filter() and Array.prototype.includes() 函数。
var filtered_array = data.results[0].address_components.filter(function(address_component){
return address_component.types.includes("administrative_area_level_2");
});
var county = filtered_array.length ? filtered_array[0].long_name: "";
您也可以使用反向地理编码直接搜索 administrative_area_level_2
$.getJSON( {
url : 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + ',' + position.coords.longitude + '&result_type=administrative_area_level_2',
data : {
sensor : false
},
success : function( data, textStatus ) {
var filtered_array = data.results[0].address_components.filter(function(address_component){
return address_component.types.includes("administrative_area_level_2");
});
var county = filtered_array.length ? filtered_array[0].long_name: "";
alert('County: ' + county);
}
});
希望对您有所帮助!
我试图从 Google 地理定位 API 结果中仅获取县名。不幸的是,结果并不总是在同一个地方,所以我需要通过一个识别键来获取它 administrative_area_level_2
这是我的电话:
$.getJSON( {
url : 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + ',' + position.coords.longitude + '&sensor=true',
data : {
sensor : false
},
success : function( data, textStatus ) {
var county = data.results[0].address_components[4].long_name;
alert('County: ' + county);
}
});
这适用于我所在的县,但不适用于其他县,因为 address_components[4]
并不总是该县。但是,我确实注意到该县始终与 administrative_area_level_2
这是一个示例响应:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Edge of Everglades Trail",
"short_name" : "Edge of Everglades Trail",
"types" : [ "route" ]
},
{
"long_name" : "Homestead",
"short_name" : "Homestead",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Miami-Dade County",
"short_name" : "Miami-Dade County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Florida",
"short_name" : "FL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "33031",
"short_name" : "33031",
"types" : [ "postal_code" ]
}
],
...
我将如何获得与 administrative_area_level_2
类型关联的 "long_name"?
为了从地址组件数组中获取 administrative_area_level_2
,您可以使用 Array.prototype.filter() and Array.prototype.includes() 函数。
var filtered_array = data.results[0].address_components.filter(function(address_component){
return address_component.types.includes("administrative_area_level_2");
});
var county = filtered_array.length ? filtered_array[0].long_name: "";
您也可以使用反向地理编码直接搜索 administrative_area_level_2
$.getJSON( {
url : 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + ',' + position.coords.longitude + '&result_type=administrative_area_level_2',
data : {
sensor : false
},
success : function( data, textStatus ) {
var filtered_array = data.results[0].address_components.filter(function(address_component){
return address_component.types.includes("administrative_area_level_2");
});
var county = filtered_array.length ? filtered_array[0].long_name: "";
alert('County: ' + county);
}
});
希望对您有所帮助!