javascript 如何“模拟”点击 Google 地图标记?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9194579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:54:06  来源:igfitidea点击:

How to "simulate" a click on a Google Maps Marker?

javascriptgoogle-maps-api-3clickhandler

提问by markzzz

What I'd like to do is to invoke the click handler on a marker. So this is my code :

我想做的是在标记上调用点击处理程序。所以这是我的代码:

var marker = new google.maps.Marker({
    position: location,
    map: map,
    title: title
});    

google.maps.event.addListener(marker, 'click', function() {
    alert("clicked");
});        

marker.click();

but I cannot see any alert...

但我看不到任何警报...

回答by Chris Broadfoot

It's possible to trigger any Maps API event listener on any object using the google.maps.event.triggerfunction.

可以使用该google.maps.event.trigger函数在任何对象上触发任何 Maps API 事件侦听器。

You'll probably want to pass in a mock MouseEventobject, depending on what your event listener(s) do with it.

您可能希望传入一个模拟MouseEvent对象,具体取决于您的事件侦听器如何处理它。

Example:

例子:

google.maps.event.trigger(marker, 'click', {
  latLng: new google.maps.LatLng(0, 0)
});

回答by Ringziii

Save your markers in an array. And do something like this:

将您的标记保存在一个数组中。并做这样的事情:

$('#anotherButton').click(function(){
   google.maps.event.trigger(marker[index], 'click');
});