2016-04-20 94 views
0

我正在开发一个网站,在该网站中绘制区域的统计数据并在Google地图上显示。从Google地图中的街道地址获取区域名称

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

但是,考虑到用户将输入地址,例如“500W133rdSt,纽约,纽约州10027,美国”,是否有可能使用一些GoogleMap API或其他东西来检索区域名称(Harlem),从提到的街道地址?

回答

0

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

对于你的榜样“500瓦第133街,纽约,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来搜索多边形。

+0

我早些时候尝试了上述内容,但并未将其用于以下两个原因。 1)它不返回哈莱姆,你可以直观地看到哈莱姆是地图上的区域。 2)当我尝试了其他几个地址时,它将sublocality_level_2作为较窄/较小的区域返回,但在诸如您的情况下,只会提到sublocality_level_1,但确实存在邻域。任何想法是什么? –

+0

我想哈林是一个多边形的空间物体,但不是地区结构的一部分(如果我们问哈林,我们将把曼哈顿作为最精确的区域): https://maps.googleapis.com/maps/ api/geocode/json?address = Harlem,New%20York,%20NY%2010027,%20USA –

+0

另一个想法是您尝试使用Nominatim(OpenStreetMap上的服务),采用类似的两步方法: Discover id:http: //nominatim.openstreetmap.org/search?q=500%20W%20133rd%20St,%20New%20York,%20NY%2010027,%20USA&format=json 使用ID获取详细信息:http://nominatim.openstreetmap。 org/details.php?place_id = 1672811647 尽管至少在Nominatim中你还没有得到Harlem的结构,你将从OpenStreetMap获得对象的ID,所以它应该更容易应用步骤 –

相关问题