CodeWhisperer
CodeWhisperer

Reputation: 1195

Empty locality by searching some cities trouch Geocoding API

When I'm searching for results by address some query results doesn't have a locality inside the JSON response code from the Geocoding API. This is what happens in example #1 when I'm searching for Houten. But when I search in example #2 on a city like Vianen then there is a locality. Is this a bug inside the API and can this be solved?

Example #1

{
  "results" : [
    {
      "address_components" : [
        {
          "long_name" : "Houten",
          "short_name" : "Houten",
          "types" : [ "administrative_area_level_2", "political" ]
        },
        {
          "long_name" : "Utrecht",
          "short_name" : "UT",
          "types" : [ "administrative_area_level_1", "political" ]
        },
        {
          "long_name" : "Nederland",
          "short_name" : "NL",
          "types" : [ "country", "political" ]
        }
      ],
      "formatted_address" : "Houten, Nederland",
      [......],
      "types" : [ "administrative_area_level_2", "political" ]
    }
  ],
  "status" : "OK"
}

Example #2

{
  "results" : [
    {
      "address_components" : [
        {
          "long_name" : "Vianen",
          "short_name" : "Vianen",
          "types" : [ "locality", "political" ]
        },
        {
          "long_name" : "Vianen",
          "short_name" : "Vianen",
          "types" : [ "administrative_area_level_2", "political" ]
        },
        {
          "long_name" : "Utrecht",
          "short_name" : "UT",
          "types" : [ "administrative_area_level_1", "political" ]
        },
        {
          "long_name" : "Nederland",
          "short_name" : "NL",
          "types" : [ "country", "political" ]
        }
      ],
      "formatted_address" : "Vianen, Nederland",
      [......],
      "place_id" : "ChIJJ8URC75jxkcRlnwIUl-xUDY",
      "types" : [ "locality", "political" ]
    }
  ],
  "status" : "OK"
}

Upvotes: 0

Views: 561

Answers (1)

xomena
xomena

Reputation: 32158

It sounds like a scoring issue on the API side. As we can see, Geocoding API prefers result of type administrative_area_level_2. For some reason the API thinks that administrative area with name Houten is more prominent result than the locality with the same name and returns the administrative area in the response.

You can try to get a place ID for locality using the place autocomplete request with filter type set to cities.

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Houten&types=(cities)&key=YOUR_API_KEY

This request returns as a first suggestion 'Houten, Netherlands' with place ID ChIJu_QGwOdmxkcRPRqtvufr2Ic and type locality. Now you can use geocoding to get this city:

https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJu_QGwOdmxkcRPRqtvufr2Ic&key=YOUR_API_KEY

The same result in Geocoder tool:

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#place_id%3DChIJu_QGwOdmxkcRPRqtvufr2Ic

I hope this workaround might be helpful.

Upvotes: 1

Related Questions