I want to add the marker on the Openlayer map using React on the center point but I am not able to do so, I tried different solutions but still no luck.
Here is my React code:
import React, { useState, useEffect, useRef } from "react";
import { Map, View } from "ol";
import TileLayer from "ol/layer/Tile";
import OSM from "ol/source/OSM";
import * as proj from "ol/proj";
import Style from "ol/style/Style";
import Icon from "ol/style/Icon";
function MapWrapper(props: any) {
const [map, setMap] = useState<any>();
const mapElement = useRef<any>();
const [center, setCenter] = useState(proj.fromLonLat([-122.335167, 47.608013]));
var iconStyle = new Style({
image: new Icon({
anchor: [0.5, 32],
anchorXUnits: "fraction",
anchorYUnits: "pixels",
src: "marker.jpg",
}),
});
useEffect(() => {
const initialMap = new Map({
target: mapElement.current,
layers: [
new TileLayer({
source: new OSM(),
}),
],
view: new View({
center: center,
zoom: 17,
}),
});
setMap(initialMap);
}, []);
return (
<div className="mapRow">
<div ref={mapElement} className="map-container" />
</div>
);
}
export default MapWrapper;
Where and how should I call the iconStyle
I am not able to get it and how to show the pointer on the assigned center point?