javascript - 从 Google geocode API 返回 Neighborhood?

标签 javascript arrays google-geocoder

我在从 google geocode api 返回社区名称时遇到问题。我只是想通过过滤来检索它,但我收到了“过滤器不是函数”错误。

我在想... filter.results(item => item.types === 'neighborhood')

但我还想返回邻域类型的long_name。

感谢您的帮助!

这是他们文档中的示例对象:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "277",
               "short_name" : "277",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Bedford Avenue",
               "short_name" : "Bedford Ave",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Williamsburg",
               "short_name" : "Williamsburg",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Brooklyn",
               "short_name" : "Brooklyn",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Kings",
               "short_name" : "Kings",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "11211",
               "short_name" : "11211",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "277 Bedford Avenue, Brooklyn, NY 11211, USA",
         "geometry" : {
            "location" : {
               "lat" : 40.714232,
               "lng" : -73.9612889
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.7155809802915,
                  "lng" : -73.9599399197085
               },
               "southwest" : {
                  "lat" : 40.7128830197085,
                  "lng" : -73.96263788029151
               }
            }
         },
         "place_id" : "ChIJd8BlQ2BZwokRAFUEcm_qrcA",
         "types" : [ "street_address" ]
      },

最佳答案

我附上了一个例子,

结果集通常会有很多地址组件,因为您过滤的条件在一个嵌套数组中,您需要先到达该数组。

下面这行会输出一个地址数组,根据你的需求你可以选择显示全部或者选择第一个。

var address = sample.results.reduce(function(prev, result) {
      return prev.concat(result.address_components.filter((item) => item.types.indexOf('route') > -1));
},[]);

var sample = {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4224764,
               "lng" : -122.0842499
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4238253802915,
                  "lng" : -122.0829009197085
               },
               "southwest" : {
                  "lat" : 37.4211274197085,
                  "lng" : -122.0855988802915
               }
            }
         },
         "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
};

var address = sample.results.reduce(function(prev, result) {
      return prev.concat(result.address_components.filter((item) => item.types.indexOf('route') > -1));
},[]);
        
console.log(address[0]," :: " , address[0].long_name ) // this will retrieve the very first filtered value

关于javascript - 从 Google geocode API 返回 Neighborhood?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51735488/

相关文章:

javascript - 离开网站时弹出窗口

javascript - HTML 日期字段无法设置回来自后端的日期字段

java - 带数组的平均值和标准差计算器

java - 如何将这些整数传递给下面的方法?

C++/阿杜伊诺 : dynamic int array

objective-c - 谷歌地图 : Error when typing in coordinates of certain countries

Javascript jQuery ajax

javascript - Redux 状态更改后 React 容器不会重新渲染

javascript - 将 json 响应获取到变量中

javascript - jquery setInterval 与 fadeOut/In