Google地图自动填充的"place_changed"以外的活动

Wes*_*ley 40 javascript jquery google-maps autocomplete google-maps-api-3

我有一个应用程序,目前正在place_changed上正确激活.

但是,我希望分支搜索在用户选择自动填充条目时以及在没有自动完成帮助的情况下自己输入文本时表现不同.

我应该使用什么样的事件监听器来区分?我无法找到有关Google地图自动填充的其他活动的任何文档.

我现在拥有的:

var gmaps = new google.maps.places.Autocomplete($("#searchproperties").get(0), 
{ types: ['geocode'], componentRestrictions: {country: 'us'} });

google.maps.event.addListener(gmaps, 'place_changed', function () {
    //FIRE SEARCH
});
Run Code Online (Sandbox Code Playgroud)

geo*_*zip 9

Google Maps Javascript API v3中只有一个针对google.maps.places.Autocomplete类的文档事件,place_changed

您可以向其添加标准HTML事件侦听器(不确定这是否会影响自动完成功能).

  • 我遇到的问题是,模糊和更改事件在Google`place_changed`事件之前触发。这使得很难知道实际发生了什么。 (2认同)

MrU*_*own 6

归结为检查您是否收到place.geometry对象,如其官方示例中所示。就如此容易!

function initialize() {

  var ac = new google.maps.places.Autocomplete(
    (document.getElementById('autocomplete')), {
      types: ['geocode']
    });

  ac.addListener('place_changed', function() {

    var place = ac.getPlace();

    if (!place.geometry) {
      // User entered the name of a Place that was not suggested and
      // pressed the Enter key, or the Place Details request failed.
      // Do anything you like with what was entered in the ac field.
      console.log('You entered: ' + place.name);
      return;
    }

    console.log('You selected: ' + place.formatted_address);
  });
}

initialize();
Run Code Online (Sandbox Code Playgroud)
#autocomplete {
  width: 300px;
}
Run Code Online (Sandbox Code Playgroud)
<input id="autocomplete" placeholder="Enter your address" type="text"></input>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
Run Code Online (Sandbox Code Playgroud)