-
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.
* CORE-4887 docs * CORE-4887 All points set * CORE-4887 Clean * CORE-4887 Add icon style * CORE-4887 Icon size * Fix loading group * Fix loading group * Fix loading group
- Loading branch information
1 parent
cf69577
commit 87fd986
Showing
5 changed files
with
88 additions
and
90 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
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,86 +1,81 @@ | ||
import React, { useRef, useEffect } from 'react' | ||
import React, { useRef, useEffect, useState } from 'react' | ||
import L from 'leaflet' | ||
import { MarkerClusterGroup } from 'leaflet.markercluster' | ||
|
||
import styles from './styles.module.css' | ||
|
||
import 'leaflet.markercluster' | ||
import { getAssetsPath } from 'utils/helpers' | ||
|
||
// somewhere in the middle of North Atlantic ocean | ||
const KEY_MOVE = 'moveend' | ||
|
||
const centerPosition = new L.LatLng(26.523257520856546, -43.10211013159716) | ||
|
||
const markerIcon = L.icon({ | ||
iconUrl: getAssetsPath('/static/images/map/marker.svg'), | ||
iconSize: [32, 32], | ||
iconSize: [18, 18], | ||
iconAnchor: [16, 32], | ||
popupAnchor: [0, -32], | ||
}) | ||
|
||
const CustomMap = ({ locations }) => { | ||
const mapContainerRef = useRef(null) | ||
const map = useRef(null) | ||
const [visibleMarkers, setVisibleMarkers] = useState([]) | ||
// const [mapLoaded, setMapLoaded] = useState(false) | ||
|
||
const mapRef = useRef(null) | ||
|
||
const loadVisibleMarkers = () => { | ||
const bounds = mapRef.current.getBounds() | ||
const inViewMarkers = locations.filter((loc) => | ||
bounds.contains(new L.LatLng(loc.latitude, loc.longitude)) | ||
) | ||
setVisibleMarkers(inViewMarkers) | ||
} | ||
|
||
useEffect(() => { | ||
const coverLayer = L.tileLayer( | ||
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', | ||
{ | ||
attribution: ` | ||
<a href="https://www.openstreetmap.org">OpenStreetMap</a> under | ||
<a href="https://creativecommons.org/licenses/by-sa/2.0">CC-BY-SA</a> | ||
`, | ||
// Initialize the map | ||
if (locations.length > 0 && !mapRef.current) { | ||
mapRef.current = L.map('map', { | ||
center: centerPosition, | ||
zoom: 2, | ||
minZoom: 1, | ||
maxZoom: 12, | ||
} | ||
) | ||
layers: [ | ||
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { | ||
attribution: | ||
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', | ||
}), | ||
], | ||
}) | ||
// Handle map movements to load new markers | ||
mapRef.current.on(KEY_MOVE, loadVisibleMarkers) | ||
} | ||
|
||
map.current = L.map(mapContainerRef.current, { | ||
center: | ||
locations[0]?.latitude && locations[0]?.longitude | ||
? new L.LatLng(locations[0]?.latitude, locations[0]?.longitude) | ||
: centerPosition, | ||
zoom: 1, | ||
maxBounds: [ | ||
[70, 180], | ||
[-70, -180], | ||
], | ||
layers: [coverLayer], | ||
scrollWheelZoom: false, | ||
}) | ||
return () => map.current.remove() | ||
// Initialize marker cluster group | ||
const markerClusterGroup = new L.MarkerClusterGroup() | ||
mapRef.current.addLayer(markerClusterGroup) | ||
|
||
return () => { | ||
// Cleanup the map and markers | ||
mapRef.current.off(KEY_MOVE, loadVisibleMarkers) | ||
mapRef.current.remove() | ||
mapRef.current = null | ||
} | ||
}, []) | ||
|
||
useEffect(() => { | ||
const markers = new MarkerClusterGroup({ | ||
chunkedLoading: true, | ||
icon: markerIcon, | ||
}) | ||
|
||
locations.forEach(({ name, href, latitude, longitude }) => { | ||
const marker = L.marker(new L.LatLng(latitude, longitude), { | ||
title: name, | ||
const markerClusterGroup = new L.MarkerClusterGroup() | ||
// Create and add markers | ||
visibleMarkers.forEach(({ name, href, latitude, longitude }) => { | ||
const marker = L.marker([latitude, longitude], { | ||
icon: markerIcon, | ||
}) | ||
if (href) { | ||
marker.bindPopup( | ||
`<a | ||
href=${href} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
${name} | ||
</a>` | ||
) | ||
} else marker.bindPopup(name) | ||
|
||
markers.addLayer(marker) | ||
}).bindPopup(href ? `<a href=${href} target="_blank">${name}</a>` : name) | ||
markerClusterGroup.addLayer(marker) | ||
}) | ||
|
||
map.current.addLayer(markers) | ||
mapRef.current.addLayer(markerClusterGroup) | ||
|
||
return () => map.current.removeLayer(markers) | ||
}, [locations]) | ||
return () => markerClusterGroup.clearLayers() | ||
}, [visibleMarkers]) | ||
|
||
return <div ref={mapContainerRef} className={styles.map} /> | ||
return <div id="map" style={{ height: '26vh', width: '100%' }} /> | ||
} | ||
|
||
export default CustomMap |
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