1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
import React, { useEffect } from 'react';
import { Map, View, Feature } from 'ol';
import { Zoom } from 'ol/control';
import TileLayer from 'ol/layer/Tile.js';
import OSM from 'ol/source/OSM.js';
import VectorLayer from 'ol/layer/Vector.js';
import VectorSource from 'ol/source/Vector.js';
import { Point } from 'ol/geom';
import { fromLonLat } from 'ol/proj.js';
import { Style, Icon } from 'ol/style.js';
import reactSvg from './assets/pin.svg?url';
const MAP = 'map' as const;
const coordinatesLondon: [number, number] = [-0.118092, 51.509865];
const coordinates = fromLonLat(coordinatesLondon);
const feature = new Feature({
geometry: new Point(coordinates),
});
const vectorLayer = new VectorLayer({
source: new VectorSource({ features: [feature] }),
style: new Style({
image: new Icon({
src: reactSvg,
anchor: [0.5, 1],
}),
}),
});
const OSMLayer = new TileLayer({ source: new OSM() });
const MapElement: React.FC = () => {
useEffect(() => {
const map = new Map({
target: MAP,
view: new View({ center: coordinates, zoom: 12 }),
layers: [OSMLayer, vectorLayer],
controls: [new Zoom()],
});
return () => map.setTarget(undefined);
}, []);
return (
<div className="mapContainer">
<div id={MAP}></div>
<div className="attribution">
© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors
</div>
</div>
);
};
|