最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get only administrative_area_level_2 from Google Geocoding API? - Stack Overflow

programmeradmin1浏览0评论

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

Here's my call:

$.getJSON( {
    url  : '=' + position.coords.latitude + ',' + position.coords.longitude + '&sensor=true',
    data : {
        sensor  : false
    },
    success : function( data, textStatus ) {
        var county = data.results[0].address_ponents[4].long_name;
        alert('County: ' + county);
    }
});

This works for the county I am in, but not for other counties because address_ponents[4] is not always the county. I did notice, however, that the county is always associated with administrative_area_level_2

Here's an example response:

{
   "results" : [
      {
         "address_ponents" : [
            {
               "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" ]
            }
         ],
... 

How would I go about getting the "long_name" associated with administrative_area_level_2 type?

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

Here's my call:

$.getJSON( {
    url  : 'http://maps.googleapis./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_ponents[4].long_name;
        alert('County: ' + county);
    }
});

This works for the county I am in, but not for other counties because address_ponents[4] is not always the county. I did notice, however, that the county is always associated with administrative_area_level_2

Here's an example response:

{
   "results" : [
      {
         "address_ponents" : [
            {
               "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" ]
            }
         ],
... 

How would I go about getting the "long_name" associated with administrative_area_level_2 type?

Share Improve this question edited Oct 7, 2019 at 7:04 xomena 32.2k6 gold badges96 silver badges125 bronze badges asked Jun 16, 2017 at 14:49 user13286user13286 3,0759 gold badges52 silver badges115 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15

In order to get administrative_area_level_2 from address ponents array you can use Array.prototype.filter() and Array.prototype.includes() functions.

var filtered_array = data.results[0].address_ponents.filter(function(address_ponent){
    return address_ponent.types.includes("administrative_area_level_2");
}); 
var county = filtered_array.length ? filtered_array[0].long_name: "";

You can also search directly the administrative_area_level_2 with reverse geocoding

$.getJSON( {
    url  : 'http://maps.googleapis./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_ponents.filter(function(address_ponent){
            return address_ponent.types.includes("administrative_area_level_2");
        }); 
        var county = filtered_array.length ? filtered_array[0].long_name: "";
        alert('County: ' + county);
    }
});

I hope this helps!

发布评论

评论列表(0)

  1. 暂无评论