Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change leaflet marker icon

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

like image 742
sowmyaa guptaa Avatar asked Jan 11 '17 11:01

sowmyaa guptaa


People also ask

How do you change the marker icon in leaflet react?

If you have the leaflet and react-leaflet npm modules installed, this should work for you. Click on the marker and you will see the popup with a "Change Marker Color" button.

How do you change the color of a leaflet icon?

You should check out Leaflet. StyleEditor on GitHub. It took me a while to figure out how to get the demo to work, but 1) click the Style tool, 2) click the marker, 3) key point... change the icon setting to something other than default, 4) select color.

What is marker in leaflet?

Use markers to call out points on the map. Marker locations are expressed in latitude/longitude coordinates, and can either appear as icons or as circles.


2 Answers

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
like image 200
Jainil Avatar answered Oct 02 '22 19:10

Jainil


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);
like image 38
Œm Pov Develop Avatar answered Oct 02 '22 19:10

Œm Pov Develop