不同状态的地理编码返回无边界的JSON

时间:2017-09-27 09:26:53

标签: json google-maps google-maps-api-3 google-geocoder

当我从http://maps.googleapis.com/maps/api/geocode/json?address=Alabama获得JSON时,它会回到我身边,如你所见:

    {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Alabama",
               "short_name" : "AL",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Alabama, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 35.008028,
                  "lng" : -84.888246
               },
               "southwest" : {
                  "lat" : 30.144425,
                  "lng" : -88.4732269
               }
            },
            "location" : {
               "lat" : 32.3182314,
               "lng" : -86.902298
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 35.008028,
                  "lng" : -84.888246
               },
               "southwest" : {
                  "lat" : 30.144425,
                  "lng" : -88.4732269
               }
            }
         },
         "place_id" : "ChIJdf5LHzR_hogR6czIUzU0VV4",
         "types" : [ "administrative_area_level_1", "political" ]
      }
   ],
   "status" : "OK"
}

但是当我输入阿拉斯加而不是阿拉巴马http://maps.googleapis.com/maps/api/geocode/json?address=Alaska

    {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "700",
               "short_name" : "700",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "East 46th Avenue",
               "short_name" : "E 46th Ave",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Midtown",
               "short_name" : "Midtown",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Anchorage",
               "short_name" : "Anchorage",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Anchorage",
               "short_name" : "Anchorage",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Alaska",
               "short_name" : "AK",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "99503",
               "short_name" : "99503",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "7402",
               "short_name" : "7402",
               "types" : [ "postal_code_suffix" ]
            }
         ],
         "formatted_address" : "700 E 46th Ave, Anchorage, AK 99503, USA",
         "geometry" : {
            "location" : {
               "lat" : 61.1789309,
               "lng" : -149.870464
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 61.1802798802915,
                  "lng" : -149.8691150197085
               },
               "southwest" : {
                  "lat" : 61.17758191970849,
                  "lng" : -149.8718129802915
               }
            }
         },
         "place_id" : "ChIJ1QZWYr2XyFYR1-572SNQDyI",
         "types" : [ "car_wash", "establishment", "finance", "point_of_interest" ]
      }
   ],
   "status" : "OK"
}

JSON中没有"bounds",为什么,我需要界限,所以我可以搜索美国各州的具体位置。 有什么不同,这种情况发生在其他一些州。 我尝试使用视口而不是边界,并再次在阿拉巴马州工作,但在阿拉斯加州,它指向安克雷奇的中心。

1 个答案:

答案 0 :(得分:2)

实际上,有一种解决方法。您需要添加'组件'您的请求的参数,然后选择“国家/地区”。用于过滤。

这是一个示例链接http://maps.googleapis.com/maps/api/geocode/json?address=Alaska&components=country:US

注意:可选择返回边界

  

bounds(可选择返回)存储可以完全包含返回结果的边界框。

这里是 documentation

希望它能帮到你解决问题。