-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: change plain js maplibre to use react-map-gl
- Loading branch information
Showing
2 changed files
with
75 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,74 @@ | ||
import { FC, useEffect, useRef } from 'react'; | ||
import maplibregl from 'maplibre-gl'; | ||
'use client'; | ||
|
||
import { FC, ReactNode, useRef, useState } from 'react'; | ||
import ReactMap, { Popup } from 'react-map-gl/maplibre'; | ||
import 'maplibre-gl/dist/maplibre-gl.css'; | ||
import { Marker } from 'react-map-gl/maplibre'; | ||
import { MapPin } from 'lucide-react'; | ||
|
||
const urgencyToColor = { | ||
alta: '#ef4444', //text-red-500 | ||
media: '#f59e0b', //text-amber-500 | ||
baja: '#10b981', //text-emerald-500 | ||
}; | ||
|
||
type MapProps = { | ||
center?: [number, number]; | ||
zoom?: number; | ||
markers?: { | ||
coordinates: [number, number]; | ||
urgency: 'alta' | 'media' | 'baja'; | ||
descriptionHTML: string; | ||
width: number; | ||
}[]; | ||
export type PinMapa = { | ||
id: string; | ||
latitude: number; | ||
longitude: number; | ||
urgency: 'alta' | 'media' | 'baja'; | ||
popup: ReactNode; | ||
}; | ||
|
||
const Map: FC<MapProps> = ({ center = [0, 0], zoom = 2, markers = [] }) => { | ||
const mapContainerRef = useRef<HTMLDivElement | null>(null); | ||
const mapRef = useRef<maplibregl.Map | null>(null); | ||
const markerRefs = useRef<maplibregl.Marker[]>([]); | ||
|
||
useEffect(() => { | ||
if (!mapRef.current) { | ||
mapRef.current = new maplibregl.Map({ | ||
container: mapContainerRef.current!, | ||
style: 'https://basemaps.cartocdn.com/gl/positron-gl-style/style.json', | ||
center: center, | ||
zoom: zoom, | ||
}); | ||
mapRef.current.addControl(new maplibregl.NavigationControl(), 'top-right'); | ||
} else { | ||
// Update center and zoom when props change | ||
mapRef.current.setCenter(center); | ||
mapRef.current.setZoom(zoom); | ||
} | ||
|
||
// Clear existing markers | ||
markerRefs.current.forEach((marker) => marker.remove()); | ||
markerRefs.current = []; | ||
|
||
// Add new markers | ||
markers.forEach((markerData) => { | ||
const marker = new maplibregl.Marker({ | ||
color: urgencyToColor[markerData.urgency], | ||
}) | ||
.setLngLat(markerData.coordinates) | ||
.setPopup( | ||
new maplibregl.Popup({ | ||
className: 'map-popup', | ||
maxWidth: `${markerData.width}px`, | ||
anchor: 'left', | ||
}).setHTML(markerData.descriptionHTML), | ||
) | ||
.addTo(mapRef.current!); | ||
type MapProps = { | ||
markers?: PinMapa[]; | ||
}; | ||
|
||
markerRefs.current.push(marker); | ||
}); | ||
const PAIPORTA_LAT = 39.42333; | ||
const PAIPORTA_LNG = -0.41667; | ||
const DEFAULT_ZOOM = 12; | ||
|
||
// Clean up function to remove markers only | ||
return () => { | ||
markerRefs.current.forEach((marker) => marker.remove()); | ||
markerRefs.current = []; | ||
}; | ||
}, [center, zoom, markers]); | ||
const Map: FC<MapProps> = ({ markers = [] }) => { | ||
const [selectedMarker, setSelectedMarker] = useState<PinMapa | null>(null); | ||
|
||
return <div ref={mapContainerRef} style={{ width: '100%', height: '75vh' }} />; | ||
console.log(selectedMarker); | ||
return ( | ||
<ReactMap | ||
initialViewState={{ | ||
longitude: PAIPORTA_LNG, | ||
latitude: PAIPORTA_LAT, | ||
zoom: DEFAULT_ZOOM, | ||
}} | ||
style={{ width: '100%', height: '75vh' }} | ||
mapStyle="https://basemaps.cartocdn.com/gl/positron-gl-style/style.json" | ||
> | ||
{markers.map((m) => { | ||
return ( | ||
<Marker | ||
key={m.id} | ||
color={urgencyToColor[m.urgency]} | ||
longitude={-0.4} | ||
latitude={39.42333} | ||
onClick={() => setSelectedMarker(m)} | ||
anchor="bottom" | ||
> | ||
<MapPin className="h-6 w-6 text-orange-500" /> | ||
</Marker> | ||
); | ||
})} | ||
{selectedMarker && ( | ||
<Popup | ||
longitude={-0.4} | ||
latitude={39.42333} | ||
className={'map-popup'} | ||
anchor="top" | ||
onClose={() => setSelectedMarker(null)} | ||
> | ||
{selectedMarker.popup} | ||
</Popup> | ||
)} | ||
</ReactMap> | ||
); | ||
}; | ||
|
||
export default Map; |