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
How to "simulate" a click on a Google Maps Marker?
提问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.trigger
function.
可以使用该google.maps.event.trigger
函数在任何对象上触发任何 Maps API 事件侦听器。
You'll probably want to pass in a mock MouseEvent
object, 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');
});