Google Maps API实施click事件以获取目标元素

时间:2019-01-30 05:46:07

标签: google-maps

以下是有关Google Maps API事件的问题:

marker.addListener('click', _.bind(function (e) {

}

在上面的代码中,我无法获取需要在其上调用函数的e.target元素。

基本上情况是,我有一个标记列表,单击一个标记时,需要触发该功能,而单击同一标记时,该功能应取消绑定。

有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

您好,我过去与标记一起工作,并且还存在相同的问题,我进行了一些研究,发现此解决方案在SO上奏效了。

您可以创建这样的功能

function createCallback(marker, callback) {
    google.maps.event.addListener(marker, 'click', function () {
     // do whatever you want to do
     callback()
    });
}

然后像这样调用它

var markerPropertyLocation = new google.maps.Marker({
    position: {markerPosition},
    map : {map},
    icon: new google.maps.MarkerImage(
            {iconImage},
            null, /* size is determined at runtime */
            null, /* origin is 0,0 */
            null, /* anchor is bottom center of the scaled image */
            new google.maps.Size(20, 27))
});
createCallback(markerPropertyLocation, function(){
 // callback funcation
});

谢谢

答案 1 :(得分:0)

为更加准确地满足我的要求,我对查询进行了重组,并将其重新发布:

在我们的Google地图实现中,我们具有以下要求:

•获得被点击标记出所有的显示在屏幕上的目标元件 •单击后放大标记,并仅将类应用于该特定标记(单击的标记)

我们编写了以下代码,但没有得到以上结果。     但是,类似的交互也可以在鼠标悬停时显示带有详细信息的tip

代码段:

<link href="//fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:bold,medium,regular" rel="stylesheet" />

<form>
  <input type='text' value='Hello world'>
  <input type='submit' value='Send form'>
</form>