如何将相同的事件侦听器添加到多个标记中,然后区分google maps api v3中侦听器中的标记? [英] how do I add same event listener to many markers and then differentiate between the markers in the listeners in google maps api v3?

查看:117
本文介绍了如何将相同的事件侦听器添加到多个标记中,然后区分google maps api v3中侦听器中的标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的javascript中有很多标记的相同事件监听器。如何区分此侦听器中的不同标记?我想在其他地方显示另一个标记,点击某个标记。每个标记都有另一个标记,我点击它显示。



事件监听器代码

  google.maps.event.addListener(marker,'click',function(){
// code goes here

});

更多详情:

我有2数组标记1和标记2,每个标记具有10个标记。我在地图上显示marker1中的10个。点击标记1 [0]标记我想在地图上显示markers2 [0]标记。我如何知道事件监听器中我点击了markers1 [0],现在我知道我可以使用THIS来识别marker1 [0],但是我怎么知道听众中它是位置为0的标记在数组markers1中,这样我也可以在数组markers2中的位置0处显示标记?解析方案

您可以轻松地添加索引(或任何其他信息)给每个标记:

  for(var i = 0; i< arrDestinations; i + = 1){ 
var marker = new google.maps.Marker({
title:arrDestinations [i] .title,
position:new google.maps.LatLng(arrDestinations [i] .lat,arrDestinations [ i] .lon),
map:map,
myIndex:i //添加的字段:每个标记包含索引
)};
bindInfoWindow(marker,map,infowindow,< p> arrDestinations [i] .description +< / p>);
}

然后在您的事件处理程序中,您可以执行此操作:

  google.maps.event.addListener(marker,'click',function(){
infowindow.setContent(html);
infowindow.open(map,marker);
setVisibility(marker ); //已添加
});

setVisibility函数将类似于由上面的150PoundsOfDonamite建议,不同之处在于您知道要显示的标记的索引:

  function setVisible(marker) {
for(var i = 0; i< markers1.length; i ++){
if(i == marker.myIndex){
markers2 [i] .setVisible(true);
} else {
markers2 [i] .setVisible(false);
}
}
}


I have the same event listener for many markers in my javascript. How do I differentiate between different markers in this listener? I want to display another marker elsewhere on clicking of a particular marker. Every marker has another marker which I display on clicking on it.

the event listener code

google.maps.event.addListener(marker, 'click', function() {
//code goes here

});

more detail:

I have 2 arrays markers1 and markers2 each having 10 markers. I display the 10 from markers1 on my map. On clicking markers1[0] marker I want to display the markers2[0] marker on the map. How do I know in the event listener that I have clicked on markers1[0], now I know that I can use the THIS for identifying markers1[0] but how do I know in the listener that it was the marker at the position 0 in array markers1 so that I could also display marker at position 0 in array markers2?

解决方案

You can easily add the index (or any other information) to each Marker:

for( var i = 0; i < arrDestinations; i += 1 ) {
    var marker = new google.maps.Marker({
        title: arrDestinations[i].title,
        position: new google.maps.LatLng(arrDestinations[i].lat, arrDestinations[i].lon),
        map: map,
        myIndex: i    // ADDED FIELD: Each marker contains its index
    )};
    bindInfoWindow(marker,map,infowindow,"<p>arrDestinations[i].description + "</p>");
}

Then in your event handler you can do this:

google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(html);
    infowindow.open(map, marker);
    setVisibility(marker);  // ADDED
});

The setVisibility function would be similar to the one suggested by 150PoundsOfDonamite above, except that you know the index of the marker that you want to make visible:

function setVisible(marker) {
    for(var i=0; i<markers1.length; i++) {
        if(i==marker.myIndex) {
            markers2[i].setVisible(true);
        } else {
            markers2[i].setVisible(false);
        }
    }
}

这篇关于如何将相同的事件侦听器添加到多个标记中,然后区分google maps api v3中侦听器中的标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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