@navigoo/map-components 1.0.6 → 1.0.8
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 +71 -15
package/package.json
CHANGED
|
@@ -29,6 +29,12 @@ const MapView: React.FC<MapViewProps> = ({
|
|
|
29
29
|
const clickMarkerRef = useRef<L.Marker | null>(null);
|
|
30
30
|
const routePolylinesRef = useRef<L.Polyline[]>([]);
|
|
31
31
|
|
|
32
|
+
// Définition des limites du Cameroun
|
|
33
|
+
const CAMEROON_BOUNDS = L.latLngBounds(
|
|
34
|
+
[1.65, 8.4], // Coin sud-ouest
|
|
35
|
+
[13.08, 16.2] // Coin nord-est
|
|
36
|
+
);
|
|
37
|
+
|
|
32
38
|
const parseWKTLineString = (wkt: string): [number, number][] => {
|
|
33
39
|
try {
|
|
34
40
|
const geo = parse(wkt);
|
|
@@ -52,18 +58,27 @@ const MapView: React.FC<MapViewProps> = ({
|
|
|
52
58
|
|
|
53
59
|
useEffect(() => {
|
|
54
60
|
if (mapContainerRef.current && !mapRef.current) {
|
|
61
|
+
const maxZoom = 18;
|
|
62
|
+
|
|
63
|
+
// Configuration de la carte avec limites strictes
|
|
55
64
|
mapRef.current = L.map(mapContainerRef.current, {
|
|
56
|
-
center: [
|
|
57
|
-
zoom:
|
|
65
|
+
center: [5.5, 12.0], // Centre du Cameroun
|
|
66
|
+
zoom: 6, // Zoom pour voir tout le Cameroun
|
|
58
67
|
minZoom: 6,
|
|
59
|
-
maxZoom:
|
|
60
|
-
maxBounds:
|
|
61
|
-
maxBoundsViscosity: 1.0,
|
|
68
|
+
maxZoom: maxZoom,
|
|
69
|
+
maxBounds: CAMEROON_BOUNDS,
|
|
70
|
+
maxBoundsViscosity: 1.0, // Empêche complètement le dépassement des limites
|
|
62
71
|
});
|
|
63
72
|
|
|
73
|
+
// Forcer la carte à rester dans les limites initiales
|
|
74
|
+
mapRef.current.setMaxBounds(CAMEROON_BOUNDS);
|
|
75
|
+
|
|
64
76
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
|
65
|
-
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
|
|
66
|
-
maxZoom:
|
|
77
|
+
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
|
|
78
|
+
maxZoom: maxZoom,
|
|
79
|
+
tileSize: 256,
|
|
80
|
+
zoomOffset: 0,
|
|
81
|
+
bounds: CAMEROON_BOUNDS, // Limiter les tuiles aux bounds du Cameroun
|
|
67
82
|
}).addTo(mapRef.current);
|
|
68
83
|
|
|
69
84
|
L.Icon.Default.mergeOptions({
|
|
@@ -74,20 +89,37 @@ const MapView: React.FC<MapViewProps> = ({
|
|
|
74
89
|
|
|
75
90
|
routeLayerRef.current = L.layerGroup().addTo(mapRef.current);
|
|
76
91
|
|
|
92
|
+
// Recentrer la carte si l'utilisateur essaie de la faire sortir des limites
|
|
93
|
+
mapRef.current.on('drag', () => {
|
|
94
|
+
if (mapRef.current) {
|
|
95
|
+
const currentCenter = mapRef.current.getCenter();
|
|
96
|
+
const currentZoom = mapRef.current.getZoom();
|
|
97
|
+
|
|
98
|
+
if (!CAMEROON_BOUNDS.contains(currentCenter)) {
|
|
99
|
+
mapRef.current.panInsideBounds(CAMEROON_BOUNDS, { animate: true });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
77
104
|
mapRef.current.on('click', async (e: L.LeafletMouseEvent) => {
|
|
78
105
|
const { lat, lng } = e.latlng;
|
|
106
|
+
|
|
107
|
+
// Vérifier que le clic est dans les limites du Cameroun
|
|
108
|
+
if (!CAMEROON_BOUNDS.contains([lat, lng])) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
79
112
|
const closestPlace = await apiClient.findClosestPlace(lat, lng);
|
|
80
113
|
const placeName = closestPlace?.name || 'Position sélectionnée';
|
|
81
114
|
|
|
82
115
|
if (clickMarkerRef.current) {
|
|
83
116
|
mapRef.current?.removeLayer(clickMarkerRef.current);
|
|
84
|
-
clickMarkerRef.current = null;
|
|
85
|
-
} else {
|
|
86
|
-
clickMarkerRef.current = L.marker([lat, lng])
|
|
87
|
-
.addTo(mapRef.current!)
|
|
88
|
-
.bindPopup(`<b>${placeName}</b><br>Lat: ${lat.toFixed(6)}<br>Lng: ${lng.toFixed(6)}`)
|
|
89
|
-
.openPopup();
|
|
90
117
|
}
|
|
118
|
+
|
|
119
|
+
clickMarkerRef.current = L.marker([lat, lng])
|
|
120
|
+
.addTo(mapRef.current!)
|
|
121
|
+
.bindPopup(`<b>${placeName}</b><br>Lat: ${lat.toFixed(6)}<br>Lng: ${lng.toFixed(6)}`)
|
|
122
|
+
.openPopup();
|
|
91
123
|
});
|
|
92
124
|
}
|
|
93
125
|
|
|
@@ -119,6 +151,12 @@ const MapView: React.FC<MapViewProps> = ({
|
|
|
119
151
|
|
|
120
152
|
// Fonction pour centrer la carte sur un point avec un marqueur
|
|
121
153
|
const centerOnPoint = async (lat: number, lng: number, placeName: string, zoom: number = 16) => {
|
|
154
|
+
// Vérifier que le point est dans les limites du Cameroun
|
|
155
|
+
if (!CAMEROON_BOUNDS.contains([lat, lng])) {
|
|
156
|
+
console.warn('Le point est en dehors des limites du Cameroun');
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
122
160
|
let displayName = placeName;
|
|
123
161
|
if (placeName === 'Votre position') {
|
|
124
162
|
const closestPlace = await apiClient.findClosestPlace(lat, lng);
|
|
@@ -203,14 +241,32 @@ const MapView: React.FC<MapViewProps> = ({
|
|
|
203
241
|
});
|
|
204
242
|
|
|
205
243
|
if (allCoordinates.length > 0) {
|
|
206
|
-
|
|
244
|
+
// Ajuster la vue pour montrer l'itinéraire, mais dans les limites du Cameroun
|
|
245
|
+
const routeBounds = L.latLngBounds(allCoordinates);
|
|
246
|
+
const paddedBounds = routeBounds.pad(0.1); // Ajouter un peu de marge
|
|
247
|
+
|
|
248
|
+
// S'assurer que les bounds ajustées sont dans les limites du Cameroun
|
|
249
|
+
const finalBounds = L.latLngBounds(
|
|
250
|
+
[
|
|
251
|
+
Math.max(paddedBounds.getSouth(), CAMEROON_BOUNDS.getSouth()),
|
|
252
|
+
Math.max(paddedBounds.getWest(), CAMEROON_BOUNDS.getWest())
|
|
253
|
+
],
|
|
254
|
+
[
|
|
255
|
+
Math.min(paddedBounds.getNorth(), CAMEROON_BOUNDS.getNorth()),
|
|
256
|
+
Math.min(paddedBounds.getEast(), CAMEROON_BOUNDS.getEast())
|
|
257
|
+
]
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
mapRef.current!.fitBounds(finalBounds);
|
|
207
261
|
}
|
|
208
262
|
} else if (searchedPlace && searchedPlace.coordinates) {
|
|
209
263
|
centerOnPoint(searchedPlace.coordinates.lat, searchedPlace.coordinates.lng, searchedPlace.name);
|
|
210
264
|
} else if (userLocation) {
|
|
211
265
|
centerOnPoint(userLocation.latitude, userLocation.longitude, 'Votre position');
|
|
212
266
|
} else {
|
|
213
|
-
|
|
267
|
+
// Retour à la vue par défaut du Cameroun
|
|
268
|
+
mapRef.current!.setView([5.5, 12.0], 6, { animate: true });
|
|
269
|
+
mapRef.current!.setMaxBounds(CAMEROON_BOUNDS);
|
|
214
270
|
}
|
|
215
271
|
}, [apiClient, userLocation, searchedPlace, routes, selectedRouteIndex, setSelectedRouteIndex]);
|
|
216
272
|
|