Skip to content

Commit 8d0b11b

Browse files
committed
Refacor marker functions
1 parent fbe3218 commit 8d0b11b

File tree

4 files changed

+24
-62
lines changed

4 files changed

+24
-62
lines changed

src/lib/Marker.tsx

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
11
import { useEffect } from 'react';
2-
import { Feature as OlFeature } from 'ol';
32
import { fromLonLat, toLonLat } from 'ol/proj';
4-
import Style from 'ol/style/Style';
5-
import Icon from 'ol/style/Icon';
6-
import Point from 'ol/geom/Point';
7-
import VectorLayer from 'ol/layer/Vector';
83
import { useMap } from './Map';
94
import { getLonLat } from './util';
10-
import VectorSource from 'ol/source/Vector';
11-
12-
export function getMarkerImage(color='red', text='A') {
13-
return `data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E` +
14-
`%3Csvg%20width%3D%2240px%22%20height%3D%2240px%22%20viewBox%3D%220%200%2015%2015%22%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E` +
15-
`%3Cpath%20fill%3D%22${color}%22%20d%3D%22M7.5%2C0C5.0676%2C0%2C2.2297%2C1.4865%2C2.2297%2C5.2703%26%23xA%3B%26%23x9%3BC2.2297%2C7.8378%2C6.2838%2C13.5135%2C7.5%2C15c1.0811-1.4865%2C5.2703-7.027%2C5.2703-9.7297C12.7703%2C1.4865%2C9.9324%2C0%2C7.5%2C0z%22%2F%3E` +
16-
`%3Ctext%20x%3D%2250%25%22%20y%3D%2250%25%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%20font-size%3D%226px%22%3E`+
17-
text +
18-
`%3C%2Ftext%3E`+
19-
`%3C%2Fsvg%3E`;
20-
}
5+
import { addMarker, removeMarker } from './marker-func';
216

227
function getNextChar(char) {
238
const ascii = char.charCodeAt(0);
@@ -29,43 +14,6 @@ function getNextChar(char) {
2914
}
3015
}
3116

32-
33-
function addMarker(markerLayer, lonLat, color, char) {
34-
const markerFeature = new OlFeature({
35-
geometry: new Point(fromLonLat(lonLat))
36-
});
37-
markerFeature.setStyle(new Style({
38-
image: new Icon({
39-
scale: 1,
40-
anchor: [0.5,40],
41-
anchorXUnits: 'fraction',
42-
anchorYUnits: 'pixels',
43-
src: getMarkerImage(color, char)
44-
}),
45-
}));
46-
47-
markerLayer.getSource().addFeature(markerFeature);
48-
return markerFeature;
49-
}
50-
51-
function getMarkerLayer(map) {
52-
const markerLayer = map.getLayers().getArray()
53-
.find(el => el.get('key') === 'markerLayer') as VectorLayer;
54-
if (markerLayer) {
55-
return markerLayer;
56-
}
57-
58-
// container of markers, this can be set when marker is added, so that I can remove it here.
59-
const newMarkerLayer = new VectorLayer({
60-
source: new VectorSource({ features: [] }),
61-
properties: {key: 'markerLayer'},
62-
zIndex: 1,
63-
});
64-
65-
map.addLayer(newMarkerLayer);
66-
return newMarkerLayer;
67-
}
68-
6917
export function Marker({
7018
lonLat=undefined,
7119
address=undefined,
@@ -80,16 +28,14 @@ export function Marker({
8028
useEffect(() => {
8129
if (!map) return;
8230

83-
const markerLayer = getMarkerLayer(map);
84-
8531
if (lonLat) {
86-
addMarker(markerLayer, lonLat, color, markerChar);
32+
addMarker(map, lonLat, color, markerChar);
8733
map.getView().setCenter(fromLonLat(lonLat))
8834
} else if (address) {
8935
getLonLat(address).then(resp => {
9036
if (resp[0]) {
9137
const lonLat = [resp[0].lon, resp[0].lat];
92-
const feature = addMarker(markerLayer, lonLat, color, markerChar);
38+
const feature = addMarker(map, lonLat, color, markerChar);
9339
feature.set('address', resp[0].display_name)
9440
map.getView().setCenter(fromLonLat(lonLat))
9541
}
@@ -99,11 +45,11 @@ export function Marker({
9945
map.on('singleclick', function (evt) {
10046
const feature = map.forEachFeatureAtPixel(evt.pixel, feature => feature);
10147
if (feature) {
102-
removeOnClick && markerLayer?.getSource().removeFeature(feature);
48+
removeOnClick && removeMarker(map, feature);
10349
} else {
10450
const clickLonLat = toLonLat(evt.coordinate);
10551
markerChar = getNextChar(markerChar);
106-
addOnClick && addMarker(markerLayer, clickLonLat, color, markerChar)
52+
addOnClick && addMarker(map, clickLonLat, color, markerChar)
10753
}
10854
});
10955
}, [map]);

src/lib/controls/SearchControl.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useEffect } from 'react';
44
import { useMap } from '../Map';
55
import { searchIcon } from './icons';
66
import './SearchControl.css';
7-
import { addMarker } from '../add-marker';
7+
import { addMarker } from '../marker-func';
88
import { fromLonLat } from 'ol/proj';
99

1010
declare let google: any;

src/lib/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ export { getAddress, getLonLat } from './util';
22

33
export { Map } from './Map';
44
export { View } from './View';
5-
export { Marker, getMarkerImage } from './Marker';
5+
export { Marker } from './Marker';
66
export { Overlay } from './Overlay';
7+
export { addMarker, removeMarker, getMarkerImage } from './marker-func';
78

89
export { LayerGroup } from './layers/LayerGroup';
910
export { TileLayer } from './layers/TileLayer';

src/lib/add-marker.ts renamed to src/lib/marker-func.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,22 @@ import { fromLonLat } from 'ol/proj';
55
import VectorSource from 'ol/source/Vector';
66
import Icon from 'ol/style/Icon';
77
import Style from 'ol/style/Style';
8-
import { getMarkerImage } from './Marker';
8+
9+
export function removeMarker(map, marker) {
10+
const markerLayer = map.getLayers().getArray()
11+
.find(el => el.get('key') === 'markerLayer') as VectorLayer;
12+
markerLayer?.getSource().removeFeature(marker);
13+
}
14+
15+
export function getMarkerImage(color='red', text='A') {
16+
return `data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E` +
17+
`%3Csvg%20width%3D%2240px%22%20height%3D%2240px%22%20viewBox%3D%220%200%2015%2015%22%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E` +
18+
`%3Cpath%20fill%3D%22${color}%22%20d%3D%22M7.5%2C0C5.0676%2C0%2C2.2297%2C1.4865%2C2.2297%2C5.2703%26%23xA%3B%26%23x9%3BC2.2297%2C7.8378%2C6.2838%2C13.5135%2C7.5%2C15c1.0811-1.4865%2C5.2703-7.027%2C5.2703-9.7297C12.7703%2C1.4865%2C9.9324%2C0%2C7.5%2C0z%22%2F%3E` +
19+
`%3Ctext%20x%3D%2250%25%22%20y%3D%2250%25%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%20font-size%3D%226px%22%3E`+
20+
text +
21+
`%3C%2Ftext%3E`+
22+
`%3C%2Fsvg%3E`;
23+
}
924

1025
export function addMarker(map, lonLat, color='red', char='') {
1126
let markerLayer = map.getLayers().getArray()

0 commit comments

Comments
 (0)