Reputation: 2353
I have a list of North American cities that are inconsistently paired with provinces and countries. Example:
I would like to format each city so it follows a consistent pattern. So that it includes City State/Province and Country. Like:
I've tried using Google's Places API to get suggestions for possible formating but all I've been able to achieve is a sort of validation that indicates if such a city exists. I was wondering if someone who has worked extensively with Google API's can guide me on how I can get City/Province-State/Country suggestions ( possibly in JSON format) from a city input.
var input = "City Name";
var service = new google.maps.places.AutocompleteService();
service.getQueryPredictions({ input: input }, callback});
I tried looking into geocoding but that seems to bring back lat/log information.
Upvotes: 2
Views: 5180
Reputation: 449385
The Geocoding API is what you want.
Try
http://maps.googleapis.com/maps/api/geocode/json?address=Calgary,%20Alberta&sensor=false
it'll return fully formatted address info in several ways (split by administrative level, formatted as an address, etc.)
{
"results" : [
{
"address_components" : [
{
"long_name" : "Calgary",
"short_name" : "Calgary",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Division No. 6",
"short_name" : "Division No. 6",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Alberta",
"short_name" : "AB",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Kanada",
"short_name" : "CA",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Calgary, Alberta, Kanada",
The formatted_address
component is in German ("Kanada") because my browser's language is German. You can define which language the output should be in using the hl
parameter.
Note that technically, according to their T&C, using the Geocoding API is permitted only to prepare data for display on a Google Map.
Upvotes: 4