简体   繁体   中英

How to change the color of a marker

Here is my code to add a marker on a map:

var marker = {'depart':null, 'arrivee':null};
var DEPART = "depart";
var ARRIVEE = "arrivee";
function addMarkerIti(statut, data, countMarker) {
        if (statut === "")
            statut = DEPART;
        var link = '<button type="submit" onclick="removeEtape('+ (statut === DEPART ? 1 : statut === ARRIVEE ? 2 : countRemoveMarker)  + ');" class="btn btn-danger btn-xs btn-block btn-popup">Supprimer ce marker</button>';
        var mm = L.marker([data.coordonnee[0], data.coordonnee[1]],  statut === DEPART ? {icon: blueIcon} : statut === ARRIVEE ? {icon: greenIcon} : {icon: yellowIcon})
        .addTo(lgMarkers).bindPopup((data.rue != "" ? "<strong>Adresse : </strong>" + (data.numero != "" ? data.numero + " " : "") + data.rue + "</br>" : "" ) 
                + (data.quartier != "" ? "<strong>Quartier : </strong>" + data.quartier + "</br>" : "")
                + (data.cp != "" ? "<strong>Code Postal : </strong>" + data.cp + "</br>" : "")
                + (data.ville != "" ? "<strong>Ville : </strong>" + data.ville + "</br>" : "")
                + "<strong>Latitude : </strong>" + data.coordonnee[0] + "</br>"
                + "<strong>Longitude : </strong>" + data.coordonnee[1] + "</br>" + link);

        if (statut === DEPART || statut === ARRIVEE) {
            marker[statut] = mm;
    }

The color of the marker is defined by {icon:blueIcon}, {icon:greenIcon} etc...

var blueIcon = new MarkerIcon({iconUrl: '../js/leaflet/images/marker-icon.png'});
var greenIcon = new MarkerIcon({iconUrl: '../js/leaflet/images/marker-icon-green.png'});
var yellowIcon = new MarkerIcon({iconUrl: '../js/leaflet/images/marker-icon-yellow.png'});

I need to make a function which changes the marker's color ({icon: ...Icon}). How can I modify the Icon of my marker ?

Thanks for your help.

Marker has a setIcon method

To make you marker yellow in your case:

marker.setIcon(yellowIcon);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM