Skip to content

Commit

Permalink
feat: enable base layer toggle
Browse files Browse the repository at this point in the history
Try new map, or revert to old map
  • Loading branch information
mxdvl committed Jun 10, 2024
1 parent 6938b3d commit 742c6a7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/components/LooMap/LooMap.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useMapState } from '../MapState';
import { MapContainer, useMapEvents } from 'react-leaflet';
import { MapContainer, TileLayer, useMapEvents } from 'react-leaflet';
import 'leaflet/dist/leaflet.css';
import { css } from '@emotion/react';
import Box from '../Box';
import { Media } from '../Media';
import Markers from './Markers';
import CurrentLooMarker from './CurrentLooMarker';
import LocateMapControl from './LocateMapControl';
import LocateMapControl, { ControlButton } from './LocateMapControl';
import { useCallback, useEffect, useState } from 'react';
import { Map } from 'leaflet';
import useLocateMapControl from './useLocateMapControl';
Expand All @@ -18,7 +18,7 @@ import React from 'react';
import router from 'next/router';
import ZoomControl from './ZoomControl';
import crosshairSvg from '../../../public/crosshair.svg';
import { leafletLayer } from 'protomaps-leaflet';
import { ProtomapLayer } from './ProtomapLayer';

const MapTracker = () => {
const [, setMapState] = useMapState();
Expand Down Expand Up @@ -74,6 +74,8 @@ const LooMap: React.FC<LooMapProps> = ({
const [announcement, setAnnouncement] = React.useState(null);
const [intersectingToilets, setIntersectingToilets] = useState([]);

const [useProtomap, setUseProtomaps] = useState(false);

const [renderAccessibilityOverlays, setRenderAccessibilityOverlays] =
useState(showAccessibilityOverlay);

Expand All @@ -100,18 +102,6 @@ const LooMap: React.FC<LooMapProps> = ({
}
}, [mapRef, mapState.map, setMapState]);

useEffect(() => {
if (!mapState.map) return;
const layer = leafletLayer({
// Free for non-commercial use https://protomaps.com/
url: 'https://api.protomaps.com/tiles/v3/{z}/{x}/{y}.mvt?key=73e8a482f059f3f5',
theme: 'white',
});
// @ts-expect-error -- this is what the docs recommend
// https://github.com/protomaps/protomaps-leaflet?tab=readme-ov-file#how-to-use
layer.addTo(mapState.map);
}, [mapState.map]);

// Begin accessibility overlay

useEffect(() => {
Expand Down Expand Up @@ -307,6 +297,17 @@ const LooMap: React.FC<LooMapProps> = ({
}
`}
>
{useProtomap ? (
<ProtomapLayer />
) : (
<TileLayer
attribution="&copy; <a href='https://www.openstreetmap.org/copyright'>OpenStreetMap</a> contributors"
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
minZoom={minZoom}
maxZoom={maxZoom}
/>
)}

{mapState.focus && <CurrentLooMarker loo={mapState.focus} />}

<Markers />
Expand All @@ -327,6 +328,21 @@ const LooMap: React.FC<LooMapProps> = ({
</Media>
</div>

<div className="leaflet-bar leaflet-bottom leaflet-left">
<ControlButton
onClick={() => {
setUseProtomaps((toggle) => !toggle);
}}
className="leaflet-control"
css={css`
padding: 5px 12px;
cursor: pointer;
`}
>
{useProtomap ? 'Revert to old map' : 'Try the new map'}
</ControlButton>
</div>

<MapTracker />

{renderAccessibilityOverlays && (
Expand Down
25 changes: 25 additions & 0 deletions src/components/LooMap/ProtomapLayer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Layer } from 'leaflet';
import { leafletLayer } from 'protomaps-leaflet';
import { useEffect } from 'react';
import { useMap } from 'react-leaflet';

export const ProtomapLayer = () => {
const map = useMap();

useEffect(() => {
// @ts-expect-error -- this works in practice
const layer: Layer = leafletLayer({
// Free for non-commercial use https://protomaps.com/
url: 'https://api.protomaps.com/tiles/v3/{z}/{x}/{y}.mvt?key=73e8a482f059f3f5',
theme: 'light',
});
// https://github.com/protomaps/protomaps-leaflet?tab=readme-ov-file#how-to-use
layer.addTo(map);

return () => {
map.removeLayer(layer);
};
}, [map]);

return null;
};

0 comments on commit 742c6a7

Please sign in to comment.