简体   繁体   English

谷歌地图自动完成自动选择第一个建议并在页面加载时自动提交搜索?

[英]Google Maps Autocomplete auto-pick first suggestion and auto-submit search on page load?

I am using Google Maps API v3 on my webpage and currently when the page loads, the search box gets pre-populated with a search term I choose.我在我的网页上使用 Google Maps API v3,目前当页面加载时,搜索框会预先填充我选择的搜索词。 But I need it to actually then search maps using that term.但我需要它来实际然后使用该术语搜索地图。 I can't seem to find any way to do this using only google's API, so I thought perhaps I could simulate an 'enter' key press using this code:我似乎无法仅使用 google 的 API 找到任何方法来执行此操作,因此我想也许我可以使用以下代码模拟“输入”按键:

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

( #pac-input is the id of the <input> tag on the map) #pac-input是地图上<input>标签的 id)

However this doesn't seem to work.但是,这似乎不起作用。

So how can I force a search on the page load?那么如何在页面加载时强制搜索?

EDIT: This is the search box I'm talking about编辑:这是我正在谈论的搜索框在此处输入图片说明

The first thing you have to think about is not which event has to be triggered, it's more important to know when to trigger the events.您首先要考虑的不是必须触发哪个事件,更重要的是知道何时触发事件。

The predictions will be loaded asynchronously, so you must wait until they are available.预测将异步加载,因此您必须等到它们可用。 There will not fire any event when the predictions are available, but you may observe the DOMNodeInserted -event of the body(the dropdown will be inserted there) and check if the nodes have the className 'pac-item' (it's the className of the items in the dropdown).当预测可用时不会触发任何事件,但您可以观察主体的DOMNodeInserted事件(下拉列表将插入那里)并检查节点是否具有 className 'pac-item'(它是下拉列表中的项目)。

Then these events must be triggered on the input:然后必须在输入上触发这些事件:

  1. keydown with keyCode:40 (to move to the first prediction in the dropdown) keydown with keyCode:40 (移动到下拉列表中的第一个预测)
  2. keydown with keyCode:13 (to select/activate the prediction) keydown with keyCode:13 (选择/激活预测)
  3. focus (to activate the input) focus (激活输入)
  4. keydown with keyCode:13 (to send the request) keydown with keyCode:13 (发送请求)

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 the listener
                              google.maps.event.removeListener(itemsloaded);
                              //trigger the 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 the DOMNodeInserted -event is supported since V9, in older versions and other browsers that didn't support this event you may check in intervals if there are .pac-item present in the document.注意:在 InternetExplorer 中,从 V9 开始支持DOMNodeInserted事件,在旧版本和其他不支持此事件的浏览器中,如果文档中存在.pac-item ,您可以检查间隔。

Demo: http://jsfiddle.net/doktormolle/R8XdL/演示: http : //jsfiddle.net/doktormolle/R8XdL/

if your use case allows, the easiest (and presumably intended by google) way of doing this is to store googles place_id and then query based on that using the new google.maps.places.PlacesService.getDetails method如果您的用例允许,最简单的(并且大概是谷歌打算这样做的)方法是存储谷歌的 place_id,然后使用新的 google.maps.places.PlacesService.getDetails 方法基于它进行查询

var service = new google.maps.places.PlacesService(map);
service.getDetails({placeId: $('[name="google_places_id"]').val()}, function(place, status) { do code here });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM