title: How to programmatically input a search string and trigger places_changed for Google Maps API?
body: <p>So I have a search page with a location input. If a user comes from another page with a search query, I want to programmatically input this query into the input and trigger a place changed. </p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>var searchBox = new google.maps.places.SearchBox(input);
  
  $('input#location').val(searchQuery);
  google.maps.event.trigger(searchBox, 'places_changed');
  </code></pre>
  
  <p>However, this gives me the error <code>Cannot read property 'length' of undefined</code> for this line of my <code>places_changed</code> function:</p>
  
  <pre><code>var places = searchBox.getPlaces();
  
  if (places.length == 0) {
    return;
  }
  </code></pre>
  
  <p>So clearly <code>searchBox</code> returns <code>undefined</code> for <code>getPlaces()</code> when the input has been filled programmatically. How can I get around this?</p>
  
  <p><strong>UPDATE:</strong> <a href="https://jsfiddle.net/3yhprm2r/" rel="noreferrer">Here is a JSFiddle</a> to exemplify what I mean.</p>