@navigoo/map-components 1.0.9 → 1.1.0
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/package.json +1 -1
- package/src/components/MapView.tsx +29 -20
package/package.json
CHANGED
|
@@ -24,17 +24,16 @@ const MapView: React.FC<MapViewProps> = ({
|
|
|
24
24
|
}) => {
|
|
25
25
|
const mapRef = useRef<L.Map | null>(null);
|
|
26
26
|
const mapContainerRef = useRef<HTMLDivElement>(null);
|
|
27
|
-
const markerRef = useRef<L.Marker | null>(null);
|
|
28
27
|
const routeLayerRef = useRef<L.LayerGroup | null>(null);
|
|
29
|
-
const clickMarkerRef = useRef<L.Marker | null>(null);
|
|
30
28
|
const routePolylinesRef = useRef<L.Polyline[]>([]);
|
|
31
29
|
|
|
32
|
-
//
|
|
30
|
+
// ✅ Bornes élargies du Cameroun
|
|
33
31
|
const CAMEROON_BOUNDS = L.latLngBounds(
|
|
34
|
-
[1.
|
|
35
|
-
[13.
|
|
32
|
+
[1.0, 7.8], // Sud-Ouest
|
|
33
|
+
[13.5, 16.5] // Nord-Est
|
|
36
34
|
);
|
|
37
35
|
|
|
36
|
+
// 🔍 Fonction pour parser les géométries WKT
|
|
38
37
|
const parseWKTLineString = (wkt: string): [number, number][] => {
|
|
39
38
|
try {
|
|
40
39
|
const geo = parse(wkt);
|
|
@@ -42,7 +41,7 @@ const MapView: React.FC<MapViewProps> = ({
|
|
|
42
41
|
return geo.coordinates.map(([lng, lat]: [number, number]) => [lat, lng] as [number, number]);
|
|
43
42
|
}
|
|
44
43
|
} catch (error) {
|
|
45
|
-
console.error('
|
|
44
|
+
console.error('Erreur de parsing WKT:', error);
|
|
46
45
|
}
|
|
47
46
|
const match = wkt.match(/LINESTRING\s*\(([^)]+)\)/);
|
|
48
47
|
if (match) {
|
|
@@ -56,25 +55,27 @@ const MapView: React.FC<MapViewProps> = ({
|
|
|
56
55
|
return [];
|
|
57
56
|
};
|
|
58
57
|
|
|
58
|
+
// 🌍 Initialisation de la carte
|
|
59
59
|
useEffect(() => {
|
|
60
60
|
if (mapContainerRef.current && !mapRef.current) {
|
|
61
61
|
const maxZoom = 18;
|
|
62
|
-
|
|
63
|
-
// 🌍 Configuration initiale de la carte
|
|
62
|
+
|
|
64
63
|
mapRef.current = L.map(mapContainerRef.current, {
|
|
65
|
-
center: [7.3697, 12.3547], // Centre
|
|
64
|
+
center: [7.3697, 12.3547], // Centre du Cameroun
|
|
66
65
|
zoom: 6,
|
|
67
|
-
minZoom: 5,
|
|
66
|
+
minZoom: 5,
|
|
68
67
|
maxZoom,
|
|
69
68
|
maxBounds: CAMEROON_BOUNDS,
|
|
70
|
-
maxBoundsViscosity:
|
|
69
|
+
maxBoundsViscosity: 0.4, // ✅ Souple — permet un léger glissement
|
|
71
70
|
});
|
|
72
71
|
|
|
72
|
+
// 🗺 Couche OpenStreetMap
|
|
73
73
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
|
74
74
|
attribution: '© OpenStreetMap contributors',
|
|
75
75
|
maxZoom,
|
|
76
76
|
}).addTo(mapRef.current);
|
|
77
77
|
|
|
78
|
+
// 🧭 Icônes par défaut
|
|
78
79
|
L.Icon.Default.mergeOptions({
|
|
79
80
|
iconRetinaUrl: 'https://unpkg.com/leaflet@1.9.4/dist/images/marker-icon-2x.png',
|
|
80
81
|
iconUrl: 'https://unpkg.com/leaflet@1.9.4/dist/images/marker-icon.png',
|
|
@@ -83,8 +84,11 @@ const MapView: React.FC<MapViewProps> = ({
|
|
|
83
84
|
|
|
84
85
|
routeLayerRef.current = L.layerGroup().addTo(mapRef.current);
|
|
85
86
|
|
|
86
|
-
//
|
|
87
|
-
|
|
87
|
+
// ✅ Recentrage automatique après affichage
|
|
88
|
+
setTimeout(() => {
|
|
89
|
+
mapRef.current?.invalidateSize();
|
|
90
|
+
mapRef.current?.fitBounds(CAMEROON_BOUNDS, { animate: true, padding: [20, 20] });
|
|
91
|
+
}, 300);
|
|
88
92
|
}
|
|
89
93
|
|
|
90
94
|
return () => {
|
|
@@ -93,10 +97,11 @@ const MapView: React.FC<MapViewProps> = ({
|
|
|
93
97
|
};
|
|
94
98
|
}, [apiClient]);
|
|
95
99
|
|
|
100
|
+
// 🚗 Affichage des itinéraires
|
|
96
101
|
useEffect(() => {
|
|
97
102
|
if (!mapRef.current) return;
|
|
98
103
|
|
|
99
|
-
// Nettoyage
|
|
104
|
+
// Nettoyage avant rendu
|
|
100
105
|
routeLayerRef.current?.clearLayers();
|
|
101
106
|
routePolylinesRef.current = [];
|
|
102
107
|
|
|
@@ -120,18 +125,22 @@ const MapView: React.FC<MapViewProps> = ({
|
|
|
120
125
|
}
|
|
121
126
|
});
|
|
122
127
|
|
|
123
|
-
// 🗺 Ajustement intelligent de la vue
|
|
128
|
+
// 🗺 Ajustement intelligent de la vue sans sortir du Cameroun
|
|
124
129
|
const routeBounds = L.latLngBounds(allCoordinates);
|
|
125
|
-
const mergedBounds = routeBounds.extend(CAMEROON_BOUNDS);
|
|
130
|
+
const mergedBounds = routeBounds.extend(CAMEROON_BOUNDS);
|
|
126
131
|
mapRef.current.fitBounds(mergedBounds, { padding: [30, 30] });
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// 🌍 Recentrage sur tout le Cameroun si aucun itinéraire
|
|
132
|
+
} else {
|
|
133
|
+
// 🌍 Recentrage sur le Cameroun si aucun itinéraire
|
|
130
134
|
mapRef.current.fitBounds(CAMEROON_BOUNDS, { animate: true });
|
|
131
135
|
}
|
|
132
136
|
}, [routes, selectedRouteIndex]);
|
|
133
137
|
|
|
134
|
-
return
|
|
138
|
+
return (
|
|
139
|
+
<div
|
|
140
|
+
ref={mapContainerRef}
|
|
141
|
+
className="w-full h-screen rounded-xl overflow-hidden shadow-md"
|
|
142
|
+
/>
|
|
143
|
+
);
|
|
135
144
|
};
|
|
136
145
|
|
|
137
146
|
export default MapView;
|