Search code examples
jsongoogle-mapsgoogle-maps-api-3google-geocoder

Geocode for different state returns JSON without bounds


When i get JSON from http://maps.googleapis.com/maps/api/geocode/json?address=Alabama it returnes me bounds as you can see:

    {
   "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"
}

but When i type Alaska instead of Alabama 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"
}

there are not "bounds" in JSON, Why, I need bounds so I can search for specific locations in the american states. What is the difference, this happens with some other states. I tried to use viewport instead of bounds and again it works on Alabama, but on Alaska it points in center of Anchorage.


Solution

  • Actually, there's a workaround. You need to add 'components' parameter to your request and select 'country' for filtering.

    Here's a sample link http://maps.googleapis.com/maps/api/geocode/json?address=Alaska&components=country:US

    Note: bounds are optionally returned

    bounds (optionally returned) stores the bounding box which can fully contain the returned result.

    Here's the documentation.

    Hope it helps your problem.