1

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

Dhiren
  • 31
  • 1
  • 9
  • 1
    I doubt that it will be possible without any hacks as you use googles own Autocomplete field. If you really want full controll then you can write your own autocomplete element with accessing https://developers.google.com/places/web-service/autocomplete directly. – mrkernelpanic May 30 '18 at 05:51

1 Answers1

1

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/

mrkernelpanic
  • 4,268
  • 4
  • 28
  • 52
  • Good answer. If you need some example with styling that looks closer to the standard autocomplete, you can [look at this fiddle](http://jsfiddle.net/upsidown/dmwv1t4e/). – MrUpsidown May 30 '18 at 07:50