I have a very standard script:
var redIcon = L.icon({
iconUrl: '1.png',
iconSize: [10, 10], // size of the icon
iconAnchor: [5, 5], // point of the icon which will correspond to marker's location
popupAnchor: [0, -5] // point from which the popup should open relative to the iconAnchor
});
But I want the iconSize to be a variable in my script:
map.on('zoomend', function () {
var ZoomLevel = map.getZoom();
var x = ZoomLevel*0.5
var y = ZoomLevel*0.5
And then:
iconSize: [x,y],
Is this even possible?
Best Answer
You could do it like this (not very elegant and IT DOES NOT WORK - see below). First add some dummy class to your icon definition to later find icons and then on
zoomend
event change icon element sizes:EDIT: This does not work correctly. Markers that use this icon change postion when zooming. Obviously some internal data interdependance is broken.
There is on other possible solution. Since icons are used for markers, there can be an array of icons defined, one size for each zoom level. Then on each zoom appropriate icon could be set for markers. All markers should be in one group layer: