change leaflet marker icon

后端 未结 4 1946
醉梦人生
醉梦人生 2021-02-20 04:17

I am using Leaflet Slider, of Dennis Wilhelm, to show changes in data on a Leaflet map.

I am trying to change the change the marker icon but not getting it right. So,How

4条回答
  •  遥遥无期
    2021-02-20 04:54

    You can create new icon class as below:

    var LeafIcon = L.Icon.extend({
        options: {
           iconSize:     [38, 95],
           shadowSize:   [50, 64],
           iconAnchor:   [22, 94],
           shadowAnchor: [4, 62],
           popupAnchor:  [-3, -76]
        }
    });
    

    Then create a new icon object like below:

    var greenIcon = new LeafIcon({
        iconUrl: 'http://leafletjs.com/examples/custom-icons/leaf-green.png',
        shadowUrl: 'http://leafletjs.com/examples/custom-icons/leaf-shadow.png'
    })
    

    Now you can above icon for the marker in the map as below:

    L.marker([51.5, -0.09], {icon: greenIcon}).addTo(map);
    

    You can refer this document for further information.

    For slidercontrol you need to create two images:

    (1) Marker Icon [ Use name: marker-icon.png ]
    (2) Marker Icon Shadow [ Use name: marker-shadow.png ]
    

    After that you can specify the default image path like below:

    L.Icon.Default.imagePath = "Url to the image folder"; // This specifies image path for marker icon. 
    e.x L.Icon.Default.imagePath = "http://leafletjs.com/examples/custom-icons";
    

    So the icon URLs will be:

    Icon URL  :  http://leafletjs.com/examples/custom-icons/marker-icon.png
    Shadow URL:  http://leafletjs.com/examples/custom-icons/marker-shadow.png
    

提交回复
热议问题