javascript - Google Maps auto search on page load -


i using google maps api v3 on webpage , when page loads, search box gets pre-populated search term choose. need search maps using term. can't seem find way using google's api, thought perhaps simulate 'enter' key press using code:

var e = jquery.event("keydown"); e.which = 13; $("#pac-input").trigger(e); 

(#pac-input id of <input> tag on map)

however doesn't seem work.

so how can force search on page load?

edit: search box i'm talking enter image description here

the first thing have think not event has triggered, it's more important know when trigger events.

the predictions loaded asynchronously, must wait until available. there not fire event when predictions available, may observe domnodeinserted-event of body(the dropdown inserted there) , check if nodes have classname 'pac-item' (it's classname of items in dropdown).

then these events must triggered on input:

  1. keydown keycode:40 (to move first prediction in dropdown)
  2. keydown keycode:13 (to select/activate prediction)
  3. focus (to activate input)
  4. keydown keycode:13 (to send request)

example:

  var input          =  document.getelementbyid('pac-input'),       ac              = new google.maps.places.searchbox(input),       itemsloaded    =  google.maps.event                           .adddomlistener(document.body,                                           'domnodeinserted',                                           function(e){                              if(e.target.classname==='pac-item'){                               //remove listener                               google.maps.event.removelistener(itemsloaded);                               //trigger events                               google.maps.event.trigger( input, 'keydown', {keycode:40})                               google.maps.event.trigger( input, 'keydown', {keycode:13})                               google.maps.event.trigger( input, 'focus')                               google.maps.event.trigger( input, 'keydown', {keycode:13})                             }                           }); 

note: in internetexplorer domnodeinserted-event supported since v9, in older versions , other browsers didn't support event may check in intervals if there .pac-item present in document.

demo: http://jsfiddle.net/doktormolle/r8xdl/


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -