Get only countries to autocomplete from Google Maps API
Asked Answered
H

4

16

I'm using Google Maps API to get autocomplete list of cities and countries (without other details), and it works exellent.

var input = document.getElementById('newAddress');
    var options = {
        types: ['(cities)']
    };

    autocomplete = new google.maps.places.Autocomplete(input, options);

Now I want to do exactly the same but to get only countries names. Somthing like replacing types: ['(cities)'] with types: ['(countries)']...
(what I tried but didn't work)

What should I do in order to get only countries into my autocomplete?

Hands answered 16/10, 2012 at 15:41 Comment(0)
C
11

There is no quick solution as Google only offers two type collections: ['(cities)'] and ['(regions)']

There is no ['(countries)'] available.

Documentation here: https://developers.google.com/places/documentation/autocomplete#place_types

EDIT:

You could as an alternative use an autocomplete plugin sourced from this url: http://www.geognos.com/api/en/countries/info/all.json

Cephalad answered 16/10, 2012 at 15:52 Comment(2)
So there is totally no some other way to get only countries?Hands
Not if you contrain yourself to Google Maps API AutocompleteCephalad
G
27

I've been playing around with the Google Autocomplete API for a bit and here's the best solution I could find for limiting your results to only countries:

var autocomplete = new google.maps.places.Autocomplete(input, options);
var result = autocomplete.getPlace();
console.log(result); // take a look at this result object
console.log(result.address_components); // a result has multiple address components

for(var i = 0; i < result.address_components.length; i += 1) {
  var addressObj = result.address_components[i];
  for(var j = 0; j < addressObj.types.length; j += 1) {
    if (addressObj.types[j] === 'country') {
      console.log(addressObj.types[j]); // confirm that this is 'country'
      console.log(addressObj.long_name); // confirm that this is the country name
    }
  }
}

If you look at the result object that's returned, you'll see that there's an address_components array which will contain several objects representing different parts of an address. Within each of these objects, it will contain a 'types' array and within this 'types' array, you'll see the different labels associated with an address, including one for country.

Glomma answered 10/6, 2014 at 15:52 Comment(2)
When you search for a city, you get multiple records related to that city, like this i.imgur.com/HopVQj3.png . Is there a way to get only the city name (a single record and not multiple records) ?Kremlin
@Kremlin what suggestions do you have for that? Have you figured that out?Corydalis
C
11

There is no quick solution as Google only offers two type collections: ['(cities)'] and ['(regions)']

There is no ['(countries)'] available.

Documentation here: https://developers.google.com/places/documentation/autocomplete#place_types

EDIT:

You could as an alternative use an autocomplete plugin sourced from this url: http://www.geognos.com/api/en/countries/info/all.json

Cephalad answered 16/10, 2012 at 15:52 Comment(2)
So there is totally no some other way to get only countries?Hands
Not if you contrain yourself to Google Maps API AutocompleteCephalad
A
0

This is something I have done using the python sdk, but it should be easily translated to any other sdk including JS:

import googlemaps


def country_search():
    gmaps = googlemaps.Client(key='Your-Key')

    # Country code is ISO standard, so it is easy to get country_name based on the code
    country_name, country_code = 'Bahamas', 'BS'
    
    # This limits the search for very few results
    result = gmaps.places_autocomplete(input_text=country_name, types='(regions)', components={'country': [country_code]})
    
    # There should be only one item that has type 'country' in `result`
    country = next(filter(lambda info: any(type == 'country' for type in info['types']), result))

    print(country["place_id"])
Assert answered 30/11, 2020 at 10:44 Comment(0)
H
-3
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
</head>
<body>
<div id="locationField">
<input id="autocomplete" placeholder="Enter your address" type="text"></input>
</div>
<div><pre><p id="data"></p></pre></div>
<script type=">
var autocomplete;
function initialize() {
autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'),{ types: ['(cities)'] });
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var address_components=autocomplete.getPlace().address_components;
var city='';
var country='';
for(var j =0 ;j<address_components.length;j++)
{
 city =address_components[0].long_name;
 if(address_components[j].types[0]=='country')
{
    country=address_components[j].long_name;
}
}
document.getElementById('data').innerHTML="City Name : <b>" + city + "</b> <br/>Country Name : <b>" + country + "</b>";
});
}
initialize();
</script>
</body>
</html>
Hedgehog answered 10/3, 2017 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.