javascript - google map marker 点击变化颜色且不影响infowindow

查看:125
本文介绍了javascript - google map marker 点击变化颜色且不影响infowindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我的代码如下:

for(i = 0; i <  test.length; i++){
 marker = new google.maps.Marker({
       position: new google.maps.LatLng(test[i].lat,test[i].long),
       map: map,
       icon:iconURL
       });
      
google.maps.event.addListener(marker, 'click', (function(marker, i) {
      return function() {
      marker = new google.maps.Marker({
      position: new google.maps.LatLng(test[i].lat,test[i].long),
      map: map,
      icon:iconClicked
                     });
      infowindow.setContent(test[i].string);
      infowindow.open(map, marker);
    }
    })(marker, i));
    }
    

我这样写,可以实现点击marker根据不同的iconURL使marker变化不同的颜色,但是当我再次点击marker的时候就没有反应了,infowindow也出不来。请问要怎样fix这个问题呢?
我估计是因为marker被覆盖的缘故。但是要怎么才能让marker被覆盖后还能触发listener呢?或者,怎样才能在不覆盖marker的情况下改变marker的颜色呢?

解决方案

自己找到了解决方案。。。其实好简单。。。在listener里面加一个marker.setIcon()就好了。。。

这篇关于javascript - google map marker 点击变化颜色且不影响infowindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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