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
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:
keydown
keycode:40 (to move first prediction in dropdown)keydown
keycode:13 (to select/activate prediction)focus
(to activate input)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.
Comments
Post a Comment