Google Autocomplete Places API not triggering event on change via TAB

后端 未结 3 368
情话喂你
情话喂你 2020-12-16 07:10

Per the Google Autocomplete Places API documentation, the getPlace() call should return a Places object with all the location data, or, if that place doesn\'t e

3条回答
  •  难免孤独
    2020-12-16 07:52

    pac-input being your place search input, you can do the following:

    // Trigger search on blur
    document.getElementById('pac-input').onblur = function () {
    
        google.maps.event.trigger(this, 'focus', {});
        google.maps.event.trigger(this, 'keydown', {
            keyCode: 13
        });
    };
    

    Or with a google maps event listener:

    // Trigger search on blur
    google.maps.event.addDomListener(document.getElementById("pac-input"), 'blur', function() {
    
        google.maps.event.trigger(this, 'focus', {});
        google.maps.event.trigger(this, 'keydown', {
            keyCode: 13
        });
    });
    

    Edit: As mentioned in the comments, this messes up with the "normal behavior" of a user selecting a place by mouse click.

    Below is a complete example using pure Javascript. For a (simpler) jQuery solution, look at @ChrisSnyder's answer.

    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.
          console.log("No details available for input: '" + place.name + "'");
          return;
        }
    
        console.log("You selected: '" + place.formatted_address + "'");
      });
    
      // Trigger search on blur
      google.maps.event.addDomListener(document.getElementById("autocomplete"), 'blur', function() {
    
        // Find the pac-container element
        var pacContainer = nextByClass(this, 'pac-container');
    
        // Check if we are hovering on one of the pac-items
        // :hover propagates to parent element so we only need to check it on the pac-container
        // We trigger the focus and keydown only if :hover is false otherwise it will interfere with a place selection via mouse click
        if (pacContainer.matches(':hover') === false) {
    
          google.maps.event.trigger(this, 'focus', {});
          google.maps.event.trigger(this, 'keydown', {
            keyCode: 13
          });
        }
    
      });
    }
    
    function hasClass(elem, cls) {
      var str = " " + elem.className + " ";
      var testCls = " " + cls + " ";
      return (str.indexOf(testCls) != -1);
    }
    
    function nextByClass(node, cls) {
      while (node = node.nextSibling) {
        if (hasClass(node, cls)) {
          return node;
        }
      }
      return null;
    }
    #autocomplete {
      width: 300px;
    }
    
    

提交回复
热议问题