开发者

Google maps api, addListener to external hover event

开发者 https://www.devze.com 2023-02-22 22:48 出处:网络
I\'m trying to add mouseenter/ mouseleave event handlers that open infowindows with a Google Map. I have ten elements, each with an associated Google Maps infowindow.I\'ve added a data-marker attrib

I'm trying to add mouseenter/ mouseleave event handlers that open infowindows with a Google Map.

I have ten elements, each with an associated Google Maps infowindow. I've added a data-marker attribute to my elements to determine which element corresponds with the respective markers/infowindows.

My problem is that I'm not constructing variables correctly within the argument list. Here it is:

 ....standard google maps definition above.  double brackets are django context variables within a forloop loop. 

infowindow[{{forloop.counter}}]= new google.maps.InfoWindow({
    content:contentString{{forloop.counter}}
    });


var marker{{forloop.counter}} = new google.maps.Marker({
    position:latlng{{forloop.counter}},
    map:map,
    icon:image,
    animation: google.maps.Animation.DROP,
     title:"Click for more info"
    });


google.maps.event.addListener(marker{{forloop.counter}},'click', function(){
    $.each(infowindow, function(name, value){
        this.close();
    }); 
    infowindow[{{forloop.counter}}].open(map,marker{{forloop.counter}});
});

{% endfor %}    
{% endif %} 

}

$('.entries').mouseenter(function(){
   var num=$(this).attr('data-mar开发者_开发知识库ker');

   $.each(infowindow, function(name, value){
    this.close();
  });

      var mk='marker'
  m=mk.concat(num);


   infowindow[num].open(map,m);

});

$('.entries').mouseleave(function(){

$.each(infowindow, function(name, value){
    this.close();
});


});

Obviously, I can't just pass a string into the .open() method and expect it to work. I'm more surprised that I can't pass the variable 'num' into the array index Thanks, Brendan


in you code infowindow[num].open(map,m); you have m as a string. m needs to be a marker object.

just build an array of markers like you did infowindows and use infowindow[num].open(map,markers[num]);

0

精彩评论

暂无评论...
验证码 换一张
取 消