Skip to content

Commit

Permalink
cherry pick f17461d
Browse files Browse the repository at this point in the history
  • Loading branch information
BenGamma committed Aug 25, 2020
1 parent 4751f56 commit 4d75866
Showing 1 changed file with 25 additions and 42 deletions.
67 changes: 25 additions & 42 deletions src/pages/Maps/Content/View/Cluster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'react-leaflet-markercluster/dist/styles.min.css'

import { createClusterIcon, createMarkerIcon } from './Sprites'

import { IPinGrouping, IMapPin } from 'src/models/maps.models'
import { IMapPin } from 'src/models/maps.models'

interface IProps {
pins: Array<IMapPin>
Expand All @@ -16,48 +16,31 @@ interface IProps {
export const Clusters: React.FunctionComponent<IProps> = ({
pins,
onPinClick,
children,
}) => {
const entities = pins.reduce(
(accumulator, pin) => {
const grouping = pin.type
if (!accumulator.hasOwnProperty(grouping)) {
accumulator[grouping] = []
}
accumulator[grouping].push(pin)
return accumulator
},
{} as Record<IPinGrouping, Array<IMapPin>>,
)

/**
* Documentation of Leaflet Clusters for better understanding
* https://github.com/Leaflet/Leaflet.markercluster#clusters-methods
*
*/
return (
<React.Fragment>
{Object.keys(entities).map(key => {
return (
<MarkerClusterGroup
iconCreateFunction={createClusterIcon({ key })}
key={key}
showCoverageOnHover={false}
spiderfyOnMaxZoom={true}
// in pixels, radius a cluster can cover
// max zoom level 18
maxClusterRadius={(zoomLevel: number) => {
return 30 - 5 * zoomLevel
}}
>
{entities[key].map(pin => (
<Marker
key={pin._id}
position={[pin.location.lat, pin.location.lng]}
icon={createMarkerIcon(pin)}
onClick={() => {
onPinClick(pin)
}}
/>
))}
</MarkerClusterGroup>
)
})}
</React.Fragment>
<MarkerClusterGroup
iconCreateFunction={createClusterIcon()}
showCoverageOnHover={false}
spiderfyOnMaxZoom={true}
// Pin Icon size is always 37x37 px
// This means max overlay of pins is 5px when not clustered
maxClusterRadius={32}
>
{pins.map(pin => (
<Marker
key={pin._id}
position={[pin.location.lat, pin.location.lng]}
icon={createMarkerIcon(pin)}
onClick={() => {
onPinClick(pin)
}}
/>
))}
</MarkerClusterGroup>
)
}

0 comments on commit 4d75866

Please sign in to comment.