@navigoo/map-components 1.0.7 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navigoo/map-components",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Reusable React components for mapping and routing in Yaoundé",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -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);
@@ -50,26 +56,29 @@ const MapView: React.FC<MapViewProps> = ({
50
56
  return [];
51
57
  };
52
58
 
53
- useEffect(() => {
59
+ useEffect(() => {
54
60
  if (mapContainerRef.current && !mapRef.current) {
55
- const maxZoom = 16;
61
+ const maxZoom = 18;
62
+
63
+ // Configuration de la carte avec limites strictes
56
64
  mapRef.current = L.map(mapContainerRef.current, {
57
- center: [7.365, 12.3], // Centre approximatif du Cameroun
58
- zoom: 7, // Zoom ajusté pour voir l'ensemble du Cameroun
65
+ center: [5.5, 12.0], // Centre du Cameroun
66
+ zoom: 6, // Zoom pour voir tout le Cameroun
59
67
  minZoom: 6,
60
68
  maxZoom: maxZoom,
61
- maxBounds: [
62
- [1.65, 8.4], // Coin sud-ouest du Cameroun
63
- [13.08, 16.2], // Coin nord-est du Cameroun
64
- ],
65
- maxBoundsViscosity: 1.0,
69
+ maxBounds: CAMEROON_BOUNDS,
70
+ maxBoundsViscosity: 1.0, // Empêche complètement le dépassement des limites
66
71
  });
67
72
 
73
+ // Forcer la carte à rester dans les limites initiales
74
+ mapRef.current.setMaxBounds(CAMEROON_BOUNDS);
75
+
68
76
  L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
69
77
  attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
70
78
  maxZoom: maxZoom,
71
79
  tileSize: 256,
72
80
  zoomOffset: 0,
81
+ bounds: CAMEROON_BOUNDS, // Limiter les tuiles aux bounds du Cameroun
73
82
  }).addTo(mapRef.current);
74
83
 
75
84
  L.Icon.Default.mergeOptions({
@@ -78,23 +87,39 @@ useEffect(() => {
78
87
  shadowUrl: 'https://unpkg.com/leaflet@1.9.4/dist/images/marker-shadow.png',
79
88
  });
80
89
 
81
-
82
90
  routeLayerRef.current = L.layerGroup().addTo(mapRef.current);
83
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
+
84
104
  mapRef.current.on('click', async (e: L.LeafletMouseEvent) => {
85
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
+
86
112
  const closestPlace = await apiClient.findClosestPlace(lat, lng);
87
113
  const placeName = closestPlace?.name || 'Position sélectionnée';
88
114
 
89
115
  if (clickMarkerRef.current) {
90
116
  mapRef.current?.removeLayer(clickMarkerRef.current);
91
- clickMarkerRef.current = null;
92
- } else {
93
- clickMarkerRef.current = L.marker([lat, lng])
94
- .addTo(mapRef.current!)
95
- .bindPopup(`<b>${placeName}</b><br>Lat: ${lat.toFixed(6)}<br>Lng: ${lng.toFixed(6)}`)
96
- .openPopup();
97
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();
98
123
  });
99
124
  }
100
125
 
@@ -126,6 +151,12 @@ useEffect(() => {
126
151
 
127
152
  // Fonction pour centrer la carte sur un point avec un marqueur
128
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
+
129
160
  let displayName = placeName;
130
161
  if (placeName === 'Votre position') {
131
162
  const closestPlace = await apiClient.findClosestPlace(lat, lng);
@@ -210,14 +241,32 @@ useEffect(() => {
210
241
  });
211
242
 
212
243
  if (allCoordinates.length > 0) {
213
- mapRef.current!.fitBounds(L.latLngBounds(allCoordinates));
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);
214
261
  }
215
262
  } else if (searchedPlace && searchedPlace.coordinates) {
216
263
  centerOnPoint(searchedPlace.coordinates.lat, searchedPlace.coordinates.lng, searchedPlace.name);
217
264
  } else if (userLocation) {
218
265
  centerOnPoint(userLocation.latitude, userLocation.longitude, 'Votre position');
219
266
  } else {
220
- mapRef.current!.setView([7.365, 12.3], 7, { animate: true });
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);
221
270
  }
222
271
  }, [apiClient, userLocation, searchedPlace, routes, selectedRouteIndex, setSelectedRouteIndex]);
223
272