从Google地图中的街道地址获取区域名称

时间:2016-04-20 05:32:41

标签: google-maps google-maps-api-3 google-maps-markers

我正在开发一个网站,用于绘制某个区域的统计数据并在Google地图上显示。

如果您在Google地图上搜索“Harlem New York,NY USA”或“University Heights Bronx,NY USA”,则会覆盖整个区域。我将展示这个领域,并将找出一种方法来显示带有统计数据的标记。

然而,鉴于用户将输入诸如“500 W 133rd St,New York,NY 10027,USA”之类的地址,是否可以使用某些Google Map API等来检索区域名称(Harlem),提到的街道地址?

1 个答案:

答案 0 :(得分:0)

您可以使用Google Maps Geocoding API (为了使用它,你需要一个Gecoding API key

为您的例子" 500 W 133rd St,New York,NY 10027,USA"您可以通过以下方式调用API: https://maps.googleapis.com/maps/api/geocode/json?address=500%20W%20133rd%20St,%20New%20York,%20NY%2010027,%20USA

如果您提供了正确的密钥,则应获得以下JSON:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "500",
               "short_name" : "500",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "West 133rd Street",
               "short_name" : "W 133rd St",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Upper Manhattan",
               "short_name" : "Upper Manhattan",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Manhattan",
               "short_name" : "Manhattan",
               "types" : [ "sublocality_level_1", "sublocality", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "New York",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "New York County",
               "short_name" : "New York County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Stany Zjednoczone",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "10027",
               "short_name" : "10027",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "7302",
               "short_name" : "7302",
               "types" : [ "postal_code_suffix" ]
            }
         ],
         "formatted_address" : "500 W 133rd St, New York, NY 10027, Stany Zjednoczone",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.8176249,
                  "lng" : -73.9532346
               },
               "southwest" : {
                  "lat" : 40.8176127,
                  "lng" : -73.9532442
               }
            },
            "location" : {
               "lat" : 40.8176127,
               "lng" : -73.9532442
            },
            "location_type" : "RANGE_INTERPOLATED",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.81896778029149,
                  "lng" : -73.9518904197085
               },
               "southwest" : {
                  "lat" : 40.81626981970849,
                  "lng" : -73.95458838029151
               }
            }
         },
         "place_id" : "Eic1MDAgVyAxMzNyZCBTdCwgTmV3IFlvcmssIE5ZIDEwMDI3LCBVU0E",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

您可以过滤掉neighborhoodsublocality_level_1postal_code类型,并获取相关的long_name来搜索多边形。