@vinctus/react-maplibre 0.2.17 → 0.2.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ErrorBoundary.js +2 -1
- package/dist/GeolocateControl.js +4 -3
- package/dist/Layer.js +7 -17
- package/dist/Map.js +14 -7
- package/dist/Marker.js +9 -11
- package/dist/NavigationControl.js +4 -3
- package/dist/Popup.js +2 -1
- package/dist/ScaleControl.js +5 -4
- package/package.json +1 -1
package/dist/ErrorBoundary.js
CHANGED
|
@@ -13,8 +13,9 @@ export class ErrorBoundary extends Component {
|
|
|
13
13
|
console.error('Uncaught map error:', error, errorInfo);
|
|
14
14
|
}
|
|
15
15
|
render() {
|
|
16
|
-
if (this.state.hasError)
|
|
16
|
+
if (this.state.hasError) {
|
|
17
17
|
return this.props.fallback || _jsx("h1", { children: "Sorry.. there was a map error" });
|
|
18
|
+
}
|
|
18
19
|
return this.props.children;
|
|
19
20
|
}
|
|
20
21
|
}
|
package/dist/GeolocateControl.js
CHANGED
|
@@ -3,16 +3,17 @@ import maplibre from 'maplibre-gl';
|
|
|
3
3
|
import { useMap } from './Map';
|
|
4
4
|
export const GeolocateControl = ({ position = 'bottom-right', options = { showUserLocation: false }, }) => {
|
|
5
5
|
const { map } = useMap();
|
|
6
|
+
const optionsString = JSON.stringify(options);
|
|
6
7
|
useEffect(() => {
|
|
7
|
-
const control = new maplibre.GeolocateControl(
|
|
8
|
+
const control = new maplibre.GeolocateControl(JSON.parse(optionsString));
|
|
8
9
|
if (map && position) {
|
|
9
10
|
map.addControl(control, position);
|
|
10
11
|
}
|
|
11
12
|
return () => {
|
|
12
|
-
if (map && map.hasControl
|
|
13
|
+
if (map && map.hasControl(control)) {
|
|
13
14
|
map.removeControl(control);
|
|
14
15
|
}
|
|
15
16
|
};
|
|
16
|
-
}, [position,
|
|
17
|
+
}, [position, optionsString, map]);
|
|
17
18
|
return null;
|
|
18
19
|
};
|
package/dist/Layer.js
CHANGED
|
@@ -14,24 +14,14 @@ export const Layer = ({ layer, source }) => {
|
|
|
14
14
|
map.addLayer(parsedLayer);
|
|
15
15
|
}
|
|
16
16
|
return () => {
|
|
17
|
-
if (
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
map.removeLayer(parsedLayer.id); // when removing, the layer must be removed first, and then the source
|
|
22
|
-
}
|
|
17
|
+
if (map) {
|
|
18
|
+
// Check if layer exists before removing
|
|
19
|
+
if (map.getLayer(parsedLayer.id)) {
|
|
20
|
+
map.removeLayer(parsedLayer.id); // when removing, the layer must be removed first, and then the source
|
|
23
21
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
try {
|
|
28
|
-
// Check if source exists before removing
|
|
29
|
-
if (map.getSource && map.getSource(parsedLayer.id)) {
|
|
30
|
-
map.removeSource(parsedLayer.id);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
catch (_b) {
|
|
34
|
-
// Source might not exist or map might be destroyed
|
|
22
|
+
// Check if source exists before removing
|
|
23
|
+
if (map.getSource(parsedLayer.id)) {
|
|
24
|
+
map.removeSource(parsedLayer.id);
|
|
35
25
|
}
|
|
36
26
|
}
|
|
37
27
|
};
|
package/dist/Map.js
CHANGED
|
@@ -44,24 +44,31 @@ export const Map = React.forwardRef((_a, ref) => {
|
|
|
44
44
|
const mapContainer = useRef(null);
|
|
45
45
|
const mapInstanceRef = useRef(null);
|
|
46
46
|
const { map, setMap, setMapLoaded } = useMap();
|
|
47
|
+
const optionsString = JSON.stringify(options);
|
|
47
48
|
useImperativeHandle(ref, () => map, [map]);
|
|
48
49
|
useEffect(() => {
|
|
49
|
-
if (map)
|
|
50
|
+
if (map) {
|
|
50
51
|
return; // initialize map only once
|
|
52
|
+
}
|
|
51
53
|
if (mapContainer.current) {
|
|
52
|
-
const
|
|
54
|
+
const parsedOptions = JSON.parse(optionsString);
|
|
55
|
+
const m = new maplibre.Map(Object.assign({ container: mapContainer.current, style: mapStyle, center: [longitude, latitude], zoom }, parsedOptions));
|
|
53
56
|
mapInstanceRef.current = m;
|
|
54
|
-
if (onDragEnd)
|
|
57
|
+
if (onDragEnd) {
|
|
55
58
|
addEventHandler(m, 'dragend', onDragEnd);
|
|
56
|
-
|
|
59
|
+
}
|
|
60
|
+
if (onLoad) {
|
|
57
61
|
addEventHandler(m, 'load', (ev) => {
|
|
58
62
|
onLoad(ev);
|
|
59
63
|
setMapLoaded(true);
|
|
60
64
|
});
|
|
61
|
-
|
|
65
|
+
}
|
|
66
|
+
if (onMoveEnd) {
|
|
62
67
|
addEventHandler(m, 'moveend', onMoveEnd);
|
|
63
|
-
|
|
68
|
+
}
|
|
69
|
+
if (onZoomEnd) {
|
|
64
70
|
addEventHandler(m, 'zoomend', onZoomEnd);
|
|
71
|
+
}
|
|
65
72
|
setMap(m);
|
|
66
73
|
// Cleanup when component unmounts
|
|
67
74
|
return () => {
|
|
@@ -69,7 +76,7 @@ export const Map = React.forwardRef((_a, ref) => {
|
|
|
69
76
|
mapInstanceRef.current = null;
|
|
70
77
|
};
|
|
71
78
|
}
|
|
72
|
-
}, [
|
|
79
|
+
}, [map, mapStyle, longitude, latitude, zoom, optionsString, onDragEnd, onLoad, onMoveEnd, onZoomEnd, setMap, setMapLoaded]);
|
|
73
80
|
const memoizedStyle = useMemo(() => (Object.assign({ position: 'relative', width: '100%', height: '100%' }, style)), [style]);
|
|
74
81
|
return (_jsx(ErrorBoundary, { fallback: fallback, children: _jsx("div", { id: id, ref: mapContainer, style: memoizedStyle, children: children }) }));
|
|
75
82
|
});
|
package/dist/Marker.js
CHANGED
|
@@ -9,7 +9,7 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
9
9
|
}
|
|
10
10
|
return t;
|
|
11
11
|
};
|
|
12
|
-
import { useEffect, useState,
|
|
12
|
+
import { useEffect, useState, useRef } from 'react';
|
|
13
13
|
import maplibre from 'maplibre-gl';
|
|
14
14
|
import { useMap } from './Map';
|
|
15
15
|
import ReactDOM from 'react-dom';
|
|
@@ -22,24 +22,22 @@ export const Marker = (_a) => {
|
|
|
22
22
|
const popupRef = useRef(null);
|
|
23
23
|
const cleanupRef = useRef(null);
|
|
24
24
|
const optionsString = JSON.stringify(options);
|
|
25
|
+
const popupString = JSON.stringify(popup);
|
|
25
26
|
useEffect(() => {
|
|
26
|
-
if (!map)
|
|
27
|
+
if (!map) {
|
|
27
28
|
return;
|
|
28
|
-
|
|
29
|
+
}
|
|
29
30
|
const element = document.createElement('div');
|
|
30
31
|
const parsedOptions = JSON.parse(optionsString);
|
|
31
|
-
|
|
32
|
-
setResult(ReactDOM.createPortal(children, element));
|
|
33
|
-
}
|
|
32
|
+
setResult(ReactDOM.createPortal(children, element));
|
|
34
33
|
// Create marker
|
|
35
|
-
const marker =
|
|
36
|
-
? new maplibre.Marker(Object.assign({}, parsedOptions))
|
|
37
|
-
: new maplibre.Marker(Object.assign(Object.assign({}, parsedOptions), { element }));
|
|
34
|
+
const marker = new maplibre.Marker(Object.assign(Object.assign({}, parsedOptions), { element }));
|
|
38
35
|
marker.setLngLat([longitude, latitude]).addTo(map);
|
|
39
36
|
markerRef.current = marker;
|
|
40
37
|
// Handle popup manually if provided
|
|
41
38
|
if (popup) {
|
|
42
|
-
const
|
|
39
|
+
const parsedPopup = JSON.parse(popupString);
|
|
40
|
+
const popupHTML = ReactDOMServer.renderToStaticMarkup(parsedPopup);
|
|
43
41
|
const handleMarkerClick = (e) => {
|
|
44
42
|
e.stopPropagation();
|
|
45
43
|
// Close existing popup if any
|
|
@@ -88,6 +86,6 @@ export const Marker = (_a) => {
|
|
|
88
86
|
markerRef.current = null;
|
|
89
87
|
}
|
|
90
88
|
};
|
|
91
|
-
}, [map, optionsString, children, longitude, latitude,
|
|
89
|
+
}, [map, optionsString, children, longitude, latitude, popupString, closeOnClick, closeButton, popupClassName]);
|
|
92
90
|
return result;
|
|
93
91
|
};
|
|
@@ -6,16 +6,17 @@ export const NavigationControl = ({ position = 'bottom-right', options = {
|
|
|
6
6
|
showCompass: false,
|
|
7
7
|
}, }) => {
|
|
8
8
|
const { map } = useMap();
|
|
9
|
+
const optionsString = JSON.stringify(options);
|
|
9
10
|
useEffect(() => {
|
|
10
|
-
const control = new maplibre.NavigationControl(
|
|
11
|
+
const control = new maplibre.NavigationControl(JSON.parse(optionsString));
|
|
11
12
|
if (map && position) {
|
|
12
13
|
map.addControl(control, position);
|
|
13
14
|
}
|
|
14
15
|
return () => {
|
|
15
|
-
if (map && map.hasControl
|
|
16
|
+
if (map && map.hasControl(control)) {
|
|
16
17
|
map.removeControl(control);
|
|
17
18
|
}
|
|
18
19
|
};
|
|
19
|
-
}, [position,
|
|
20
|
+
}, [position, optionsString, map]);
|
|
20
21
|
return null;
|
|
21
22
|
};
|
package/dist/Popup.js
CHANGED
package/dist/ScaleControl.js
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
2
|
import maplibre from 'maplibre-gl';
|
|
3
3
|
import { useMap } from './Map';
|
|
4
|
-
export const ScaleControl = ({ position = 'bottom-right', options
|
|
4
|
+
export const ScaleControl = ({ position = 'bottom-right', options }) => {
|
|
5
5
|
const { map } = useMap();
|
|
6
|
+
const optionsString = JSON.stringify(options);
|
|
6
7
|
useEffect(() => {
|
|
7
|
-
const control = new maplibre.ScaleControl(
|
|
8
|
+
const control = new maplibre.ScaleControl(JSON.parse(optionsString));
|
|
8
9
|
if (map && position) {
|
|
9
10
|
map.addControl(control, position);
|
|
10
11
|
}
|
|
11
12
|
return () => {
|
|
12
|
-
if (map && map.hasControl
|
|
13
|
+
if (map && map.hasControl(control)) {
|
|
13
14
|
map.removeControl(control);
|
|
14
15
|
}
|
|
15
16
|
};
|
|
16
|
-
}, [position,
|
|
17
|
+
}, [position, optionsString, map]);
|
|
17
18
|
return null;
|
|
18
19
|
};
|