Search code examples
google-mapsgoogle-maps-api-3geocodinggoogle-geocoding-apigoogle-maps-static-api

When querying the Google Maps API I get identical results for "New York, United States" and "New York, NY, United States"


We have an embedded search bar in our site which utilizes the Google Maps API for both its suggestions and results. When a user searches for "New York" and specifically clicks on the suggestion which indicates that they are searching for New York State this api call is made:

https://maps.googleapis.com/maps/api/js/GeocodeService.Search?4sNew%20York%2C%20United%20States&7sUS&8m2&1scountry&2sUS&9sen-US&callback=xdc._ohwoca&token=44216

When they just type in "New York" and hit enter, or select the suggestion which denotes "New York, NY", this api call is made:

https://maps.googleapis.com/maps/api/js/GeocodeService.Search?4sNew%20York%2C%20NY%2C%20United%20States&7sUS&8m2&1scountry&2sUS&9sen-US&callback=xdc._n2ga8u&token=62787

The problem is that the returned JSON is identical between the two, with the exception of the callback header:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "New York",
               "short_name" : "New York",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "New York, NY, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.9175771,
                  "lng" : -73.70027209999999
               },
               "southwest" : {
                  "lat" : 40.4773991,
                  "lng" : -74.25908989999999
               }
            },
            "location" : {
               "lat" : 40.7127837,
               "lng" : -74.0059413
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.9152555,
                  "lng" : -73.70027209999999
               },
               "southwest" : {
                  "lat" : 40.4960439,
                  "lng" : -74.25573489999999
               }
            }
         },
         "place_id" : "ChIJOwg_06VPwokRYv534QaPC8g",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

This results in incorrect search results provided to our users. Specifically, we key off of the "types" field. Since both documents specify that the user searched for a 'locality' we always return results for New York City rather than New York State as may have been specified.

Is this a bug in the Google Maps API? Do we need to specify some flag to force a stricter interpretation of what we're sending? Other ideas?

Note, if you perform this search on the user facing www.google.com/maps, you will get NY State rather than NYC. I don't know enough about how that page differs from the API to know what's going on there.


Solution

  • I ended up opening an issue with the Google Maps team. According to their engineers, this is the expected behavior. They believe that most users intend to see "New York City" specifically when searching for "New York" rather than the state. We ended up catching our users' queries and forcing State searches in our systems iff they specified "New York" without an additional "City" parameter.