Search code examples
google-maps-api-3autocompletecountrygoogle-places-autocompletegoogle-suggest

Google autocomplete suggestion without country name


I am looking for a possibility to get suggestions without country name.

I have the following js code:

var options = {
    types: ['(cities)'],
        componentRestrictions: {country: "us"}
     };
autocomplete = new google.maps.places.Autocomplete((document.getElementById('autocomplete')), options);

The result is e.g. Austin TX, USA

Now I want to have for that example only

Expect result is e.g. Austin TX

See more info following image link click here

Do you have an idea? Thanks. Dhiraj Gangal


Solution

  • I investigated more on this and I have more Info here:

    The Service to use: https://developers.google.com/maps/documentation/javascript/places-autocomplete?hl=de#place_autocomplete_service

    The AutocompletePrediction defines what the service returns https://developers.google.com/maps/documentation/javascript/reference?hl=de#AutocompletePrediction

    and a PredictionTerm here https://developers.google.com/maps/documentation/javascript/reference?hl=de#PredictionTerm is one part of the "description" that gets intially displayed as result.

    The interesting part is actually the PredictionTerm which is a part of the whole place suggestion.

    If you look into this example: https://developers.google.com/maps/documentation/javascript/examples/places-queryprediction

    You could easily grab each PredictionTerm of the suggestion results like in the following snippet:

    predictions.forEach(function(prediction) {
         var li = document.createElement('li');
         var city = prediction.terms[2];
         var content = "Results: "+city;
         li.appendChild(document.createTextNode(prediction.description));
         document.getElementById('results').appendChild(li);
    });
    

    I made an example: https://jsfiddle.net/yfkpvf72/1/