Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map marker overlap #2530

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions src/features/areaAssignments/components/OrganizerMapRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'react-leaflet';
import { Box, Divider, lighten, Typography } from '@mui/material';
import { FC, useContext, useEffect, useRef, useState } from 'react';
import { FeatureGroup as FeatureGroupType, latLngBounds } from 'leaflet';
import { FeatureGroup as FeatureGroupType } from 'leaflet';

import { DivIconMarker } from 'features/events/components/LocationModal/DivIconMarker';
import ZUIAvatar from 'zui/ZUIAvatar';
Expand All @@ -19,9 +19,9 @@ import {
ZetkinLocation,
} from '../types';
import { ZetkinArea } from 'features/areas/types';
import objToLatLng from 'features/areas/utils/objToLatLng';
import { assigneesFilterContext } from './OrganizerMapFilters/AssigneeFilterContext';
import isPointInsidePolygon from '../../canvass/utils/isPointInsidePolygon';
import { getBoundSize } from '../../canvass/utils/getBoundSize';

const LocationMarker: FC<{
areaAssId: string;
Expand Down Expand Up @@ -308,24 +308,9 @@ const OrganizerMapRenderer: FC<OrganizerMapRendererProps> = ({
return -1;
} else {
// When none of the two areas are selected, sort them
// by size, so that big ones are underneith and the
// by size, so that big ones are underneath and the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

// smaller ones can be clicked.
const bounds0 = latLngBounds(a0.points.map(objToLatLng));
const bounds1 = latLngBounds(a1.points.map(objToLatLng));

const dimensions0 = {
x: bounds0.getEast() - bounds0.getWest(),
y: bounds0.getNorth() - bounds0.getSouth(),
};
const dimensions1 = {
x: bounds1.getEast() - bounds1.getWest(),
y: bounds1.getNorth() - bounds1.getSouth(),
};

const size0 = dimensions0.x * dimensions0.y;
const size1 = dimensions1.x * dimensions1.y;

return size1 - size0;
return getBoundSize(a1) - getBoundSize(a0);
}
})
.map((area) => {
Expand Down Expand Up @@ -398,7 +383,11 @@ const OrganizerMapRenderer: FC<OrganizerMapRendererProps> = ({
return (
<>
{overlayStyle == 'households' && (
<DivIconMarker iconAnchor={[0, 0]} position={mid}>
<DivIconMarker
iconAnchor={[0, 0]}
position={mid}
zIndexOffset={100}
k-nut marked this conversation as resolved.
Show resolved Hide resolved
>
<Box
bgcolor="white"
borderRadius={1}
Expand All @@ -420,7 +409,11 @@ const OrganizerMapRenderer: FC<OrganizerMapRendererProps> = ({
</DivIconMarker>
)}
{overlayStyle == 'progress' && stats && (
<DivIconMarker iconAnchor={[0, 0]} position={mid}>
<DivIconMarker
iconAnchor={[0, 0]}
position={mid}
zIndexOffset={100}
>
<Box
bgcolor="white"
borderRadius={1}
Expand Down Expand Up @@ -453,7 +446,11 @@ const OrganizerMapRenderer: FC<OrganizerMapRendererProps> = ({
</DivIconMarker>
)}
{overlayStyle == 'assignees' && hasPeople && (
<DivIconMarker iconAnchor={[0, 0]} position={mid}>
<DivIconMarker
iconAnchor={[0, 0]}
position={mid}
zIndexOffset={100}
>
{detailed && (
<Box
alignItems="center"
Expand Down
11 changes: 11 additions & 0 deletions src/features/canvass/utils/getBoundSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { latLngBounds } from 'leaflet';

import { ZetkinArea } from '../../areas/types';
import objToLatLng from '../../areas/utils/objToLatLng';

export const getBoundSize = (area: ZetkinArea): number => {
const bounds0 = latLngBounds(area.points.map(objToLatLng));
const dx = bounds0.getEast() - bounds0.getWest();
const dy = bounds0.getNorth() - bounds0.getSouth();
return dx * dy;
};
29 changes: 19 additions & 10 deletions src/features/events/components/LocationModal/DivIconMarker.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { LatLngExpression, divIcon, LeafletEventHandlerFnMap } from 'leaflet';
import { FC, ReactNode, useMemo } from 'react';
import { divIcon } from 'leaflet';
import { FC, useMemo } from 'react';
import { createPortal } from 'react-dom';
import { Marker } from 'react-leaflet';
import { Marker, MarkerProps } from 'react-leaflet';

export const DivIconMarker: FC<{
children: ReactNode;
draggable?: boolean;
eventHandlers?: LeafletEventHandlerFnMap;
iconAnchor?: [number, number];
position: LatLngExpression;
}> = ({ children, draggable, eventHandlers, iconAnchor, position }) => {
export const DivIconMarker: FC<
{
iconAnchor?: [number, number];
} & Pick<
MarkerProps,
'children' | 'draggable' | 'eventHandlers' | 'position' | 'zIndexOffset'
>
> = ({
Comment on lines +6 to +12
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dit is aba geil

children,
draggable,
eventHandlers,
iconAnchor,
position,
zIndexOffset,
}) => {
const iconDiv = useMemo(() => document.createElement('div'), []);
return (
<>
Expand All @@ -22,6 +30,7 @@ export const DivIconMarker: FC<{
iconAnchor,
})}
position={position}
zIndexOffset={zIndexOffset}
/>
{createPortal(children, iconDiv)}
</>
Expand Down
28 changes: 7 additions & 21 deletions src/features/geography/components/GeographyMap/MapRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Box, useTheme } from '@mui/material';
import { FeatureGroup, latLngBounds } from 'leaflet';
import { FeatureGroup } from 'leaflet';
import { FC, useEffect, useRef, useState } from 'react';
import {
FeatureGroup as FeatureGroupComponent,
Expand All @@ -11,7 +11,7 @@ import {

import { PointData, ZetkinArea } from 'features/areas/types';
import { DivIconMarker } from 'features/events/components/LocationModal/DivIconMarker';
import objToLatLng from 'features/areas/utils/objToLatLng';
import { getBoundSize } from '../../../canvass/utils/getBoundSize';

type Props = {
areas: ZetkinArea[];
Expand Down Expand Up @@ -141,27 +141,13 @@ const MapRenderer: FC<Props> = ({
.filter(
(area) => area.id != editingArea?.id && area.id != selectedArea?.id
)
// Sort areas by size, so that big ones are underneath and the
// smaller ones can more easily be clicked.
.map((area) => ({ area, size: getBoundSize(area) }))
.sort((a0, a1) => {
// Sort areas by size, so that big ones are underneith and the
// smaller ones can more easily be clicked.
const bounds0 = latLngBounds(a0.points.map(objToLatLng));
const bounds1 = latLngBounds(a1.points.map(objToLatLng));

const dimensions0 = {
x: bounds0.getEast() - bounds0.getWest(),
y: bounds0.getNorth() - bounds0.getSouth(),
};
const dimensions1 = {
x: bounds1.getEast() - bounds1.getWest(),
y: bounds1.getNorth() - bounds1.getSouth(),
};

const size0 = dimensions0.x * dimensions0.y;
const size1 = dimensions1.x * dimensions1.y;

return size1 - size0;
return a1.size - a0.size;
})
.map((area) => {
.map(({ area }) => {
// The key changes when selected, to force redraw of polygon
// to reflect new state through visual style
const key = area.id + '-default';
Expand Down
Loading