简体   繁体   English

单张自定义标记图标缩放以缩放

[英]Leaflet custom marker icon scale to zoom

I use Leaflet to draw an OpenStreetMap and attach it with a custom icon marker 我使用Leaflet绘制OpenStreetMap并使用自定义图标标记附加它

var mymap = L.map('mapid').setView([x, y], 13);

    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
        attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
        maxZoom: 18,
        id: ID,
        accessToken: accessToken
    }).addTo(mymap);

    var busIcon = new L.Icon({
        iconUrl: 'images/marker/bus.png',
        // shadowUrl: 'images/leaflet/marker-shadow.png',
        iconSize: [12, 12],
        iconAnchor: [12, 41],
        popupAnchor: [1, -34],
        // shadowSize: [41, 41]
    });

    var marker = L.marker([x, y],{icon:busIcon}).addTo(mymap);

    mymap.on('zoomed', function() {
        var currentZoom = mymap.getZoom();
        busIcon = new L.Icon({
            iconUrl: 'images/marker/bus.png',
            iconSize: [mymap.getZoom*2, mymap.getZoom*2],
            iconAnchor: [12, 41],
            popupAnchor: [1, -34],
        });
        marker.setIcon(busIcon);
    });

Now I want to resize my marker icon depending on zoom level. 现在我想根据缩放级别调整标记图标的大小。 There's something wrong in my above code. 我上面的代码有问题。 What should I do? 我该怎么办? If marker is a CircleMaker , there is a setRadius function for me to handle this with ease. 如果marker是CircleMaker ,则有一个setRadius函数可供我轻松处理。 But in this case the marker is a custom icon, I tried as above and failed. 但在这种情况下,标记是一个自定义图标,我尝试如上,但失败了。 How to fix it? 怎么解决?

You have a typo: zoomed should be zoomend 你有一个错字:缩放应该是zoomend

map.on('zoomend', function() {
});

http://plnkr.co/edit/72ywrO8pgmmxLW6Y8mcL?p=preview http://plnkr.co/edit/72ywrO8pgmmxLW6Y8mcL?p=preview

Apart from that, I would create and keep the icons out of the zoomend callback. 除此之外,我会创建并保持图标不受zoomend回调的影响。

As it is, your code is creating an icon each time zoom is changing. 实际上,每次缩放变化时,您的代码都会创建一个图标。

As YaFred said, there were some typos like zoomend , but also mymap.getZoom that should be mymap.getZoom() 正如YaFred所说,有一些像zoomend这样的拼写错误,还有mymap.getZoom应该是mymap.getZoom()

I made a new answer to this old question to propose a more efficient solution. 我对这个老问题做了一个新的答案,提出了一个更有效的解决方案。 You can add a className to your icons (see leaflet documentation ). 您可以在图标中添加className (请参阅传单文档 )。 This means we can edit the height and width of the icon through css! 这意味着我们可以通过CSS编辑图标的高度和宽度! In your zoomend function, instead of creating a new icon, simply call this JQuery: 在你的zoomend函数中,只需调用这个JQuery,而不是创建一个新图标:

var newzoom = '' + (2*(mymap.getZoom())) +'px';
$('#mapid .YourClassName').css({'width':newzoom,'height':newzoom}); 

For larger amounts of markers, this will significantly improve your performance as mentioned in this stackoverflow question: Leaflet custom icon resize on zoom. 对于更大量的标记,这将显着提高您的性能,如此stackoverflow问题中所述: Leaflet自定义图标在缩放时调整大小。 Performance icon vs divicon 性能图标与divicon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM