Skip to content

Commit

Permalink
Merge pull request #2423 from zetkin/undocumented/make-map-move-better
Browse files Browse the repository at this point in the history
Make map tiles and polygons render correctly on pan/zoom
  • Loading branch information
richardolsson authored Dec 17, 2024
2 parents 935a023 + 47dc263 commit b1fba82
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/features/canvassAssignments/components/CanvassAssignmentMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
latLngBounds,
LatLngBounds,
LatLngTuple,
LeafletMouseEvent,
} from 'leaflet';
import { makeStyles } from '@mui/styles';
import { GpsNotFixed } from '@mui/icons-material';
Expand Down Expand Up @@ -79,18 +80,20 @@ const CanvassAssignmentMap: FC<CanvassAssignmentMapProps> = ({
assignment.organization.id,
assignment.id
);
const [localStorageBounds, setLocalStorageBounds] = useLocalStorage<
[LatLngTuple, LatLngTuple] | null
>(`mapBounds-${assignment.id}`, null);

const [map, setMap] = useState<Map | null>(null);
const [zoomed, setZoomed] = useState(false);
const [selectedPlaceId, setSelectedPlaceId] = useState<string | null>(null);
const [isCreating, setIsCreating] = useState(false);
const [created, setCreated] = useState(false);

const [map, setMap] = useState<Map | null>(null);
const crosshairRef = useRef<HTMLDivElement | null>(null);
const reactFGref = useRef<FeatureGroupType | null>(null);

const [localStorageBounds, setLocalStorageBounds] = useLocalStorage<
[LatLngTuple, LatLngTuple] | null
>(`mapBounds-${assignment.id}`, null);
const selectedPlace = places.find((place) => place.id == selectedPlaceId);

const saveBounds = () => {
const bounds = map?.getBounds();
Expand All @@ -103,10 +106,6 @@ const CanvassAssignmentMap: FC<CanvassAssignmentMapProps> = ({
}
};

const [zoomed, setZoomed] = useState(false);

const selectedPlace = places.find((place) => place.id == selectedPlaceId);

const updateSelection = useCallback(() => {
let nearestPlace: string | null = null;
let nearestDistance = Infinity;
Expand Down Expand Up @@ -180,22 +179,22 @@ const CanvassAssignmentMap: FC<CanvassAssignmentMapProps> = ({

useEffect(() => {
if (map) {
map.on('click', (evt) => {
const handlePan = (evt: LeafletMouseEvent) => {
panTo(evt.latlng);
});
};
map.on('click', handlePan);

map.on('move', () => {
updateSelection();
});
map.on('move', updateSelection);

map.on('moveend', saveBounds);

map.on('zoomend', () => saveBounds);
map.on('zoomend', saveBounds);

return () => {
map.off('move');
map.off('moveend');
map.off('movestart');
map.off('click', handlePan);
map.off('move', updateSelection);
map.off('moveend', saveBounds);
map.off('zoomend', saveBounds);
};
}
}, [map, selectedPlaceId, places, panTo, updateSelection]);
Expand Down

0 comments on commit b1fba82

Please sign in to comment.