change leaflet marker icon

51,129

Solution 1

This will be the exact changes in the original slidercontrol.js file

   if (this._layer) {
        var index_temp = 0;
        this._layer.eachLayer(function (layer) {

             var greenIcon = L.icon({ //add this new icon
                iconUrl: i+'.png',
                shadowUrl: 'leaf-shadow.png',

                iconSize:     [38, 95], // size of the icon
                shadowSize:   [50, 64], // size of the shadow
                iconAnchor:   [22, 94], // point of the icon which will correspond to marker's location
                shadowAnchor: [4, 62],  // the same for the shadow
                popupAnchor:  [-3, -76] // point from which the popup should open relative to the iconAnchor
            });

            options.markers[index_temp] = layer;
            options.markers[index_temp].setIcon(greenIcon); //Here set the icon to indiviual markers

            ++index_temp;
        });
        options.maxValue = index_temp - 1;
        this.options = options;
    }

Solution 2

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

Solution 3

var century21icon = L.icon({
    iconUrl: 'https://i.ibb.co/sJrMTdz/favicon-32x32.png',
    iconSize: [20, 20]
    });
var maker = L.marker([25.045403,121.526088],{icon: century21icon}).addTo(map);
Share:
51,129
sowmyaa guptaa
Author by

sowmyaa guptaa

Updated on October 27, 2020

Comments

  • sowmyaa guptaa
    sowmyaa guptaa over 3 years

    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 can I change marker icon when use Leaflet Slider to show changes over time? What changes I have to do in the original SliderControl.js?

    Thanks in advance!

    Below is the link to Dennis Wilhelm's Leaflet Slider code:

    https://github.com/dwilhelm89/LeafletSlider/blob/master/SliderControl.js

  • sowmyaa guptaa
    sowmyaa guptaa over 7 years
    Actually, if you see the original slidercontrol.js file (github.com/dwilhelm89/LeafletSlider/blob/master/…), here it adds the marker like.. map.addLayer(_options.markers[i]); and not like L.marker([51.5, -0.09], {icon: greenIcon}).addTo(map); so, how to change the icon in map.addLayer(_options.markers[i])?
  • Run_Script
    Run_Script about 4 years
    Welcome to stackoverflow and thanks for answering. Could you add a short text or explanation to your answer?