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

 于志军贵冰卿 发布于 2023-02-12 16:35

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

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

HTML:


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和自动完成控件有什么区别?他们不做同样的事情吗?

1 个回答
  • 对于SearchBox,您必须在输入字段上使用谷歌地图的触发事件,如此

    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', {});
    
    };
    

    2023-02-12 16:37 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有