简体   繁体   中英

How to change marker color in leaflet?

I have a method which works in google maps but in leaflet doesn't work. I show what method return in the picture below:

I want to change color with my method. Now I have a default icon

private getMarkerIcon(marker: OpMarker): any {
        if (marker.iconImage) {
            if (marker.iconImage.indexOf('data:image/') > -1)
                return marker.iconImage
            return 'data:image/*;base64,' + marker.iconImage
        } 
        else if (marker.iconURL) {
            return marker.iconURL
        }
        else {

            let iconConfig: any = null


            if (this.$scope.options.defaultMarkerIconOptions != null) {
                iconConfig = _.clone(this.$scope.options.defaultMarkerIconOptions)
                let color: string = null

                if (marker.fillColor) {
                    color = marker.fillColor

                    //TODO: add alpha (opacity)
                    if (color.charAt(0) === '#') {
                        color = color.replace('#', '').slice(0, 6)
                    }

                    if (color.length > 6) {
                        color = color.slice(0, 6)
                        console.warn('Colors in the RGBA model are not supported!')
                    }
                }

                if (color != null) {
                    iconConfig.fillColor = `#${color}`
                }
            }

            return iconConfig
        }
    }

and my method where create a new marker

const icon: any = this.getMarkerIcon(marker)

        if (icon == null) {
            console.warn(StreetMapProvider.LOG_STRING, "No default marker icon options. Using the default leaflet maps one.")
        }

        let mapMarker: any = new L.marker([marker.coords.latitude, marker.coords.longitude], {iconUrl: icon})

给您的图标类并更改其样式

marker._icon.classList.add("yourclass");

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