【问题标题】:change marker icon on mouseover ( google maps V3 )在鼠标悬停时更改标记图标(谷歌地图 V3)
【发布时间】:2012-01-02 04:10:46
【问题描述】:
var image = 'bullets/_st_zzzzzzl SSS.gif';

var bar1 = new google.maps.Marker({
    position: myLatLng, 
    map: map,
    icon: image,
    title: "bar number 1"       
}); 

    google.maps.event.addListener(bar1, 'mouseover', function() {
        infowindow.open(map,bar1);
    });

    google.maps.event.addListener(bar1, 'mouseout', function() {
        infowindow.close(map,bar1);
    });

现在,当我在鼠标悬停时,我希望图标更改为我得到的另一个图像。 我尝试了一些技巧和一些代码,但没有任何效果...... 感谢您的帮助

【问题讨论】:

    标签: google-maps-api-3


    【解决方案1】:

    使用marker.setIcon() 函数。其余的几乎与您的代码中打开/关闭信息窗口相同:

    var icon1 = "imageA.png";
    var icon2 = "imageB.png";
    
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: icon1,
        title: "some marker"
    });
    
    google.maps.event.addListener(marker, 'mouseover', function() {
        marker.setIcon(icon2);
    });
    google.maps.event.addListener(marker, 'mouseout', function() {
        marker.setIcon(icon1);
    });
    

    请注意,除了在setIcon() 函数中使用图像路径外,您还可以使用google.maps.MarkerImage 对象,这非常有用,特别是如果您想使用图像精灵。

    【讨论】:

    • 能否请您提供精灵的解决方案。我尝试使用setIcon() 在鼠标悬停时更改图标原点,但这不起作用
    【解决方案2】:
    google.maps.event.addListener(marker, 'mouseover', function() {
        infowindow.open(map, this);
    });
    
    // assuming you also want to hide the infowindow when user mouses-out
    google.maps.event.addListener(marker, 'mouseout', function() {
        infowindow.close();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-09
      • 2012-02-13
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      相关资源
      最近更新 更多