javascript - 谷歌地图 - 点击按钮触发搜索框

标签 javascript google-maps google-maps-api-3

我有兴趣实现一个旁边带有“提交”/“开始”按钮的 Google map 搜索按钮(例如在 http://maps.google.com 上)。如果我在搜索框中按 Enter,一切都很好,但我不知道如何使用按钮强制/提交搜索。

现在我的代码基于以下示例:https://developers.google.com/maps/documentation/javascript/examples/places-searchbox .最重要的部分是我现在正在使用它:

HTML:

<div id="intro-search">
    <input id="search-box" />
    <button type="button">Submit!</button>
</div>

JS:

  // Listen for the event fired when the user selects an item from the
  // pick list. Retrieve the matching places for that item.
  google.maps.event.addListener(searchBox, 'places_changed', function() {
    // ... 
    // Show the results on the map
    // ...
  }

我不知道如何在单击按钮时触发同样的事情。

另一方面,SearchBox 之间有什么区别?和 Autocomplete控制?他们不做同样的事情吗?

最佳答案

对于搜索框,你必须像这样在输入字段上使用谷歌地图的触发事件

document.getElementById('my-button').onclick = function () {
    var input = document.getElementById('my-input');

    google.maps.event.trigger(input, 'focus', {});
    google.maps.event.trigger(input, 'keydown', { keyCode: 13 });
    google.maps.event.trigger(this, 'focus', {});

};

关于javascript - 谷歌地图 - 点击按钮触发搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20407045/

相关文章:

android - 如何在android中显示我当前位置的对象的方向(箭头)

javascript - 单击其他时更改 Google map 标记图标

javascript - Autofit 谷歌使用地点 ID 映射多个标记

javascript - Extjs - 在选项卡标题上对齐图标

Java ScriptEngine 找不到 Javascript 文件引用的模块

javascript - 如果我去 '/contact' ,我想保留以前的路线 View 但显示模态

java - 在java中通过Google获取地址经度和纬度的最佳方法是什么

javascript - 灰度 Google map

java - 在 GWT 生产模式下加载带有标记的谷歌地图时出现异常

javascript - 使用带有 MathML 输入和 CSS/HTML 渲染的 MathJax 显示带有文本输入的数学公式