如何从Google Geocoding API中仅获取Administrative_area_level_2? [英] How to get only administrative_area_level_2 from Google Geocoding API?
问题描述
我正在尝试从Google地理位置API结果中仅获取县名.不幸的是,该县并不总是位于同一地点,因此我需要通过识别键administrative_area_level_2
I am attempting to get only the county name from a Google geolocation API result. Unfortunately, the county is not always in the same spot for the results so I need to grab it by an identifying key 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
This works for the county I am in, but not for other counties because address_components[4]
is not always the county. I did notice, however, that the county is always associated with 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"?
How would I go about getting the "long_name" associated with administrative_area_level_2
type?
推荐答案
为了从地址组件数组中获取administrative_area_level_2
,您可以使用 Array.prototype.includes()函数.
In order to get administrative_area_level_2
from address components array you can use Array.prototype.filter() and Array.prototype.includes() functions.
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
You can also search directly the administrative_area_level_2
with reverse geocoding
$.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 Geocoding API中仅获取Administrative_area_level_2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!