如何绘制谷歌地图邻域边界?

时间:2014-02-04 09:07:23

标签: api map

我想通过JavaScript V3 Map Api绘制Google定义的邻域边界。我找到了它,但无法为此找到更好的解决方案。如果您浏览Google地图并搜索任何位置,Google会以“红色”颜色突出显示边界。所以我想要相同但绘制多边形线。

1 个答案:

答案 0 :(得分:1)

我能想到的最接近的是使用谷歌地理编码并从中获取一个边界框。例如,如果您查询http://maps.googleapis.com/maps/api/geocode/json?address=southwark,则Google地理编码会提供以下内容,

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Southwark",
               "short_name" : "Southwark",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Greater London",
               "short_name" : "Gt Lon",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "United Kingdom",
               "short_name" : "GB",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "SE1",
               "short_name" : "SE1",
               "types" : [ "postal_code_prefix", "postal_code" ]
            },
            {
               "long_name" : "London",
               "short_name" : "London",
               "types" : [ "postal_town" ]
            }
         ],
         "formatted_address" : "Southwark, London, Greater London SE1, UK",
         "geometry" : {
            "location" : {
               "lat" : 51.502781,
               "lng" : -0.087738
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 51.50972609999999,
                  "lng" : -0.07173059999999999
               },
               "southwest" : {
                  "lat" : 51.4958349,
                  "lng" : -0.1037454
               }
            }
         },
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

在这里,您可以使用“geometry”对象来获取结果的边界框。