简体   繁体   中英

google map place API how to remove country name from suggestion

When we type a location or address the autocomplete will show the country name in its suggestion. Is there way to remove or omit country name from suggestions?

在此处输入图片说明

You can configure the AutocompletePrediction object returned by AutocompleteService . The relevant properties of AutocompletePrediction are:

  • description (Type: string. This is the unformatted version of the query suggested by the Places service.)
  • terms (Type: Array Information about individual terms in the above description, from most to least specific. For example, "Taco Bell", "Willitis", and "CA".)

So you can modify the response changing one of the two properties, for instance removing the country by splicing the description prediction.description.split(", ").slice(0,-1)

For the example below I used a JQuery UI widget as a wrapper for the AutocompleteService following https://stackoverflow.com/a/13774273/2314737

 $(function() { $("#search").autocomplete({ source: function(request, response) { var service = new google.maps.places.AutocompleteService(); service.getPlacePredictions({ input: request.term, type: 'geocode', }, function(predictions, status) { if (status != google.maps.places.PlacesServiceStatus.OK) { alert(status); return; } response($.map(predictions, function(prediction, i) { return { value: prediction.description, //label: prediction.terms[0].value label: prediction.description.split(", ").slice(0,-1), } })); }); }, }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=placeshttps://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script> <div class="ui-widget"> <label for="search">Search:</label> <input id="search"> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM