从自动填充中仅获取城市名称

时间:2019-07-10 09:14:19

标签: javascript google-maps-api-3 autocomplete

我希望用户从 Google Maps Autocomplete API 中仅选择城市名称。如果城市与他们的国家一起列出,这不是问题,但是当用户选择一个城市时,我不想在逗号后得到任何东西。有什么办法可以从选择中删除该国名?

var placeSearch, fromLocation, whereLocation;
var script = document.createElement("script");
script.src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyBcWtvH1d2_fltDXR0aC7brC56QQZqXaWA&libraries=places&callback=activatePlacesSearch";
document.head.append(script);

function activatePlacesSearch() {
  fromLocation = new google.maps.places.Autocomplete(
    (document.getElementById('fromLoc')), {
      types: ['(cities)']
    });

  whereLocation = new google.maps.places.Autocomplete(
    (document.getElementById('whereLoc')), {
      types: ['(cities)']
    });
}
<div class="col-md align-items-end">
  <div class="form-group">
    <label for="#">From</label>
    <div class="form-field">
      <div class="icon" style="width: 0px;"><span class="icon-my_location"></span></div>
      <input id="fromLoc" name="from" type="text" class="form-control" placeholder="From">
    </div>
  </div>
</div>
<div class="col-md align-items-end">
  <div class="form-group">
    <label for="#">Where</label>
    <div class="form-field">
      <div class="icon" style="width: 0px;"><span class="icon-map-marker"></span></div>
      <input id="whereLoc" name="where" type="text" class="form-control" placeholder="Where">
    </div>
  </div>
</div>
<script>
</script>
<script type="text/javascript" src=""></script>

0 个答案:

没有答案