Search code examples
openlayers-5

Openlayers 5 - how to set for only 1 marker with a different icon / image?


In my app I use regular markers. One marker should be different.

Using OL5, how can I add this different marker with a specific icon / image?

Looking at (older?) examples I see this? But this will fail in OL5.

const marker = new Feature({
    geometry: new Point(fromLonLat([0 + mylongitude, 0 + (mylatitude)]))
});
marker.setStyles(new Style({
    image: new Icon(({
        crossOrigin: 'anonymous',
        src: 'assets/images/guestimate_red_hair.png'
    }))
}));
this.vectorLayer.getSource().addFeature(marker);

Solution

  • you can use icon style for that .

        var iconStyle = new ol.style.Style({
          image: new ol.style.Icon(({
            anchor: [0.5, 0.5],
            scale:0.15,
            opacity: 0.75,
            src: 'assets/images/guestimate_red_hair.png'
          }))
        });
    marker.setStyle(iconStyle)
    

    check out this https://openlayers.org/en/latest/apidoc/module-ol_style_Image-ImageStyle.html

    make sure that image url is correct.