0
votes

I have a google maps application where users can search by Country, State, City or a street address. Users may be anywhere in the world and they may be searching for anywhere else in the world, not just within their own country.

I need the geocoder to have a bias such that if a state is entered (without the country) it geocodes to the state and not to a city with the same name. Our application prioritises countries first, then states, then cities etc... however the geocoder is not doing the same.

Eg. I want to search for "Victoria" which is a state within Australia. http://maps.googleapis.com/maps/api/geocode/json?address=victoria shows Victoria, BC, Canada. http://maps.googleapis.com/maps/api/geocode/json?address=victoria&region=au shows the state of Victoria in Australia however I cannot include the region as my users may be anywhere in the world so I have no way of knowing which region they are searching for.

I have looked at "administrative levels" and also "types" but I cannot find a solution which suits my needs of simply prioritising in the order country > state > city.

I ideally want something like this: maps.googleapis.com/maps/api/geocode/json?address=victoria&components=administrative_area:WILDCARD

OR

maps.googleapis.com/maps/api/geocode/json?address=victoria&types=administrative_area_level_1

Of course neither of these solutions work but I hope they illustrate what I am trying to achieve.

Any suggestions?

Thanks,

Nicole

1

1 Answers

0
votes

You can do a query without specifying the address, use

...?components=administrative_area:victoria 

and then iterate over the results.address_components to pick out ones where the types include administrative_area_level_1

Update: I noticed that depending on the search term provided to administrative_area, google is using some kind of heuristics to determine the certainty of the results. If there's a clear winner, then only 1 result is shown. If the matching is similar for a group of locations, then you will get several. So when there's several results, you can pick towards a higher or lower administrative_area_level to suit your needs.