Skip to content
Advertisement

How to color vue2 leaflet markers dynamically?

I have an google-maps_pin.svg file which represents a google-maps pin.

I would like to color the different markers (generated with v-for) based on categories (restaurant, art gallery, etc.) dynamically.

Is there a way to achieve that with vue2 leaflet?

Here’s my map:

JavaScript

The color I want to give to the individual pins is stored in markedLocation.info.category.color variable.

Advertisement

Answer

Yes you can implement by using this property L.divIcon().

Represents a lightweight icon for markers that uses a simple <div> element instead of an image. Inherits from Icon but ignores the iconUrl and shadow options.

Your template will be like bellow

JavaScript

and your getIcon() method will be like this

JavaScript

You could check here with working JS fiddle and please see below screenshot taken from JS fiddle.

enter image description here

Advertisement