2015-12-19 12 views

回答

0

您可以配置通過AutocompleteService返回AutocompletePrediction object。的AutocompletePrediction相關屬性是:

  • description(類型:。字符串這是商家信息服務提出的查詢未格式化的版本)
  • terms(類型:Array 關於上述各方面信息說明,從多到少具體。例如,「塔可鍾」,「Willitis」和「CA」。)

所以,你可以修改響應不斷變化的兩個屬性之一,例如移除該國通過拼接描述prediction.description.split(", ").slice(0,-1)

對於下面我用一個jQuery UI插件的例子作爲AutocompleteService的包裝以下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>

相關問題