【问题标题】:Leaflet mouseout event on marker标记上的传单 mouseout 事件
【发布时间】:2016-05-24 08:42:21
【问题描述】:

我编写此代码是为了在鼠标悬停时更改标记的图标,并在鼠标悬停时将其改回,但鼠标悬停后似乎永远不会触发 mouseout 事件。
我也提到了这个问题(Leaflet Mouseout called on MouseOver event),但我不知道这是否是这里的问题。如果这是问题,我应该如何解决它。

    L.marker([20.123,40,234],{icon:icon}).on('mouseover',function(e){
        this.setIcon(highlight);
    }).on('mouseout',function(e){
        this.setIcon(icon);
    }).addTo(map);

编辑 1: 这是完整的代码:

    var map = L.map('map').fitWorld();
    L.tileLayer("https://api.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}",{           
        id:'mapankit.137364c3', 
        accessToken:'pk.eyJ1IjoibWFwYW5raXQiLCJhIjoiY2lramo5anZoMDdjMnVjajdjYWtqbXZ3eiJ9.uR_6t2C2f5Ib9qOPnt_l8Q'}).addTo(map);

    var icon = L.divIcon({
        html : '<svg width="12px" height="12px"><g><path class="outer" d="M-5 0 A5 5 0 0 1 5 0 A5 5 0 0 1 -5 0" style="stroke-opacity: 1; stroke-width: 2; fill: white; stroke: rgb(0, 198, 228);" transform="translate(6,6)"></path><path class="inner" d="M-2.5 0 A2.5 2.5 0 0 1 2.5 0 A2.5 2.5 0 0 1 -2.5 0" style="stroke-opacity: 1; stroke-width: 0; fill: white; stroke: white;" transform="translate(6,6)"></path></g></svg>',
        className : 'foo',
        iconAnchor : L.point(6,6)
    });

    var highlight = L.divIcon({
        html : '<svg width="25px" height="25px"><g><path class="outer" d="M-10 0 A10 10 0 0 1 10 0 A10 10 0 0 1 -10 0" style="fill: white; stroke: rgb(0, 198, 228); stroke-width: 2;" transform="translate(12,12)"></path><path class="inner" d="M-5 0 A5 5 0 0 1 5 0 A5 5 0 0 1 -5 0" style="fill: rgb(0, 198, 228); stroke: rgb(0, 198, 228); stroke-opacity: 1;" transform="translate(12,12)"></path></g></svg>',
        className : 'bar',
        iconAnchor : L.point(12,12)
    })

    L.marker([20.123,40,234],{icon:icon}).on('mouseover',function(e){
        this.setIcon(highlight);
    }).on('mouseout',function(e){
        this.setIcon(icon);
    }).addTo(map);

【问题讨论】:

  • 是的,你是对的,它不适用于 DivIcon(但它适用于 Icon)
  • 我有动态内容用作图标,因此我使用 divIcon 以便我可以直接提供 HTML
  • 有什么办法可以将 html 赋予图标而不使用 divIcon?
  • SVG UI 事件可能无法正确处理。您是否尝试过在DivIcon 中使用非 SVG 内容?
  • 是的,效果很好。我尝试将 svg 作为文件提供给有效的图标。但在我的用例中,我无法为我创建的每个新图标创建文件。

标签: javascript leaflet


【解决方案1】:

我知道这个问题已经很老了,但我也遇到了这个问题并且想出了一个很好的解决方案。不要将事件侦听器直接添加到DivIcon,而是将它们应用于元素。

var map = ...

var normal = ...

var highlight = ...

function updateIcon(marker, icon) {
    marker.setIcon(icon)
    $(marker.getElement()).on({
        'mouseenter': (e) => { updateIcon(marker, highlight); $(this).off() },
        'mouseleave': (e) => { updateIcon(marker, normal);    $(this).off() }
    })
}

var marker = L.marker([20.123,40,234]).addTo(map)

updateIcon(marker, normal)

这对我来说非常有效。我的设置有点不同,因为它都在 VueJS 内部,但一般机制是相同的。如果需要任何调整,请告诉我。

【讨论】:

    【解决方案2】:

    你不能这样做,setIcon 不能在 L.DivIcon 中动态工作,只有 L.Icon。 但如果一个是图标,你可以让它工作:

    var highlight = L.divIcon({
            html: '<svg width="25px" height="25px"><g><path class="outer" d="M-10 0 A10 10 0 0 1 10 0 A10 10 0 0 1 -10 0" style="fill: black; stroke: rgb(0, 198, 228); stroke-width: 2;" transform="translate(12,12)"></path><path class="inner" d="M-5 0 A5 5 0 0 1 5 0 A5 5 0 0 1 -5 0" style="fill: rgb(0, 198, 228); stroke: rgb(0, 198, 228); stroke-opacity: 1;" transform="translate(12,12)"></path></g></svg>',
            className: 'foo',
            iconAnchor: [12, 12]
        });
        var myIcon = L.icon({
            iconUrl: 'my-icon.png',
            iconRetinaUrl: 'my-icon@2x.png',
            shadowUrl: 'my-icon-shadow.png',
            shadowRetinaUrl: 'my-icon-shadow@2x.png'
    
        });
    
        var x = L.marker([20.123, 40, 234], { icon: myIcon }).addTo(currentView);
        x.on("mouseover", function () {            
            this.setIcon(myIcon);
        });
    
        x.on("mouseout", function () {
            this.setIcon(highlight);
        });
    

    【讨论】:

    • 它不是动态的有什么特别的原因吗?
    • 因为 setIcon 是一种需要 Icon 才能工作的方法,所以当您使用 DivIcon 时,您就打破了逻辑。在我的例子中,我打破了逻辑,在做恢复之后:P
    • 但是divIcon 只不过是icon 的扩展。 L.DivIcon = L.Icon.extend({ options: {来自源link
    • 关于这个,它是关于标记如何工作​​的,icon 是一个新的 L.Icon,当你调用 divIcon 时,你会更改选项,当你调用 Icon 时,你会恢复选项。我不知道如何换句话说 D:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 2017-06-28
    • 1970-01-01
    相关资源
    最近更新 更多