-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
133 additions
and
17 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useRef, useEffect } from 'react'; | ||
import { Icon, Marker, layerGroup } from 'leaflet'; | ||
import 'leaflet/dist/leaflet.css'; | ||
import { useMap } from './use-map.ts'; | ||
import { City } from '../../dataTypes/city.ts'; | ||
import { Point } from '../../dataTypes/point.ts'; | ||
|
||
interface MapProps { | ||
city: City; | ||
points: Point[]; | ||
selectedPoint: Point | undefined; | ||
} | ||
|
||
const defaultCustomIcon = new Icon({ | ||
iconUrl: 'public/img/pin.svg', | ||
iconSize: [40, 40], | ||
iconAnchor: [20, 40], | ||
}); | ||
|
||
const currentCustomIcon = new Icon({ | ||
iconUrl: 'public/img/pin-active.svg', | ||
iconSize: [40, 40], | ||
iconAnchor: [20, 40], | ||
}); | ||
|
||
export function Map(props: MapProps): JSX.Element { | ||
const { city, points, selectedPoint } = props; | ||
|
||
const mapRef = useRef(null); | ||
const map = useMap(mapRef, city); | ||
|
||
useEffect(() => { | ||
if (map) { | ||
const markerLayer = layerGroup().addTo(map); | ||
points.forEach((point) => { | ||
const marker = new Marker({ | ||
lat: point.location.latitude, | ||
lng: point.location.longitude, | ||
}); | ||
|
||
marker | ||
.setIcon( | ||
selectedPoint !== undefined && point.id === selectedPoint.id | ||
? currentCustomIcon | ||
: defaultCustomIcon, | ||
) | ||
.addTo(markerLayer); | ||
}); | ||
|
||
return () => { | ||
map.removeLayer(markerLayer); | ||
}; | ||
} | ||
}, [map, points, selectedPoint]); | ||
|
||
return <section className="cities__map map" ref={mapRef}></section>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useEffect, useState, MutableRefObject, useRef } from 'react'; | ||
import { Map, TileLayer } from 'leaflet'; | ||
import { City } from '../../dataTypes/city.ts'; | ||
|
||
export function useMap( | ||
mapRef: MutableRefObject<HTMLElement | null>, | ||
city: City, | ||
): Map | null { | ||
const [map, setMap] = useState<Map | null>(null); | ||
const isRenderedRef = useRef<boolean>(false); | ||
|
||
useEffect(() => { | ||
if (mapRef.current !== null && !isRenderedRef.current) { | ||
const instance = new Map(mapRef.current, { | ||
center: { | ||
lat: city.location.latitude, | ||
lng: city.location.longitude, | ||
}, | ||
zoom: 10, | ||
}); | ||
|
||
const layer = new TileLayer( | ||
'https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png', | ||
{ | ||
attribution: | ||
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>', | ||
}, | ||
); | ||
|
||
instance.addLayer(layer); | ||
|
||
setMap(instance); | ||
isRenderedRef.current = true; | ||
} | ||
}, [mapRef, city]); | ||
|
||
return map; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Location } from './location.ts'; | ||
|
||
export type Point = { | ||
location: Location; | ||
id: string; | ||
}; |
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