如何“模拟”点击Google地图标记? [英] How to "simulate" a click on a Google Maps Marker?

查看:116
本文介绍了如何“模拟”点击Google地图标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

marker.click();

但我看不到任何提醒...

解决方案

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

你可能需要传递一个模拟 MouseEvent 对象,具体取决于你的事件监听器



示例:

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


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...

解决方案

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

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

Example:

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

这篇关于如何“模拟”点击Google地图标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆