I'm trying to add an icon on the map with OpenLayers 5.
I've tried to follow the Openlayers sample on the site, but without success (https://openlayers.org/en/latest/examples/icon.html)
I believe that the problem may be the Style, because when I remove it from the feature, a point is shown on the map, however when I try to add a style to that Point (that it is the icon), nothing is shown on the map.
I send below the code that I use:
import Point from 'ol/geom/Point'
import { Icon, Style } from 'ol/style.js'
// or
// import Icon from 'ol/style/Icon'
// import Style from 'ol/style/Style'
...
const vectorMarkerSource = new VectorSource()
const vectorMarkerGroup = new VectorLayer({
source: vectorMarkerSource
})
...
this.olmap = new Map({
target: 'map',
layers: [
baseLayerGroup, vectorMarkerGroup
],
view: this.view
})
...
var iconFeature = new Feature({
geometry: new Point([0, 0]),
projection: 'EPSG:4326'
})
// I've already tried the two options of 'src'
var iconStyle = new Style({
image: new Icon(/** @type {module:ol/style/Icon~Options} */ ({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: '@/assets/img/marker/marker-blue.png'
// src: '../../assets/img/marker/marker-blue.png'
}))
})
iconFeature.setStyle(iconStyle)
vectorMarkerSource.addFeature(iconFeature)
Thank you in advance.
I have discovered a solution accidentally. I was reading another code to solve another problem and on this code the creator used the following approach to insert an icon.
import markerIconBlue from '@/assets/img/marker/marker-icon-2x-blue.png'
var iconStyle = new Style({
image: new Icon(/** @type {module:ol/style/Icon~Options} */ ({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: markerIconBlue
}))
})
Fortunately, this approach have worked to me and I hope that this help others.