简体   繁体   English

在google.maps.event.addlistener(自动完成,...)中注册“点击”事件?

[英]Registering a 'click' event with google.maps.event.addlistener(autocomplete, …)?

I'd like my program to do something following the event of a user clicking on a google maps autocomplete suggestion item - something along the lines of the following: 我希望我的程序在用户单击google maps自动完成建议项后执行某些操作-类似于以下内容:

var autocomplete = new google.maps.places.Autocomplete($("#input_field")[0], {});

google.maps.event.addListener(autocomplete, 'click', function() {
    // do something
});

The common practice is to use 'place_changed' instead of 'click', but that's too general for my purposes, as it also captures the event of selecting an autocomplete box by scrolling over it with the up/down arrow keys. 通常的做法是使用“ place_changed”而不是“ click”,但这对我而言太笼统了,因为它还捕获了通过使用向上/向下箭头键滚动选择自动完成框的事件。

I'd like to specifically capture the event that a user clicks (or presses, on a mobile device) a particular autocomplete suggestion. 我想特别记录用户单击 (或在移动设备上按)一个特定的自动完成建议的事件。 What's the correct way to do this? 正确的方法是什么?

Not sure what you meant by "click", but the click UI event in Google Maps Javascript API means user clicks on the map view. 不确定“点击”是什么意思,但是Google Maps Javascript API中的click UI事件表示用户点击了地图视图。 A general use case for click UI event is to draw a marker on the map view, when user click(or touch/tap on mobile device) on the map view. click UI事件的一般用例是,当用户在地图视图上单击(或在移动设备上触摸/轻击)时,在地图视图上绘制标记。 You can refer to this page for more information about Google Maps Javascript API UI Events. 您可以参考此页面以获取有关Google Maps Javascript API UI事件的更多信息。

So if you want to perform a place autocomplete when user clicks/taps on the map view, it will not be a really good user experience. 因此,如果您要在用户单击/轻按地图视图时执行地点自动完成功能,那将不是真正好的用户体验。 In addition, you can not know the name(or text) of the place when your user clicks/taps on the map view. 此外,当用户在地图视图上单击/轻击时,您将不知道该地点的名称(或文字)。

Furthermore, the place_changed event means user clicks/taps/selects an item from the suggestion list. 此外, place_changed事件意味着用户单击/轻击/选择建议列表中的项目。 You can put a log statement inside your place_changed callback, and you will see the log will be printed when users clicks/taps/selects an item from the suggestion list. 您可以在place_changed回调中放置一条日志语句,当用户单击/轻击/从建议列表中选择一个项目时,您将看到该日志将被打印。 I am not sure why this does not give you what you need. 我不确定为什么这不能满足您的需求。

You can refer to these link for place_changed demo: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-hotelsearch 您可以参考以下链接获取place_changed演示: https : //developers.google.com/maps/documentation/javascript/examples/places-autocomplete-hotelsearch

https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete

Or this JSFiddle link, it shows a popup dialog when you clicks/taps/selects an item from suggestion list: https://jsfiddle.net/btc67xwp/ 或此JSFiddle链接,当您单击/轻击/从建议列表中选择一个项目时,它会显示一个弹出对话框: https : //jsfiddle.net/btc67xwp/

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

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