Skip to content

Commit da63bb0

Browse files
author
Sine Jespersen
authored
Merge pull request #56 from itk-dev/feature/3175-street-map-bounds
Feature/3175 street map bounds
2 parents bd23f0f + 7280cc9 commit da63bb0

File tree

7 files changed

+82
-64
lines changed

7 files changed

+82
-64
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- [PR-56](https://github.com/itk-dev/aapodwalk/pull/56)
11+
- Rename component `Map.jsx` -> `MapComponent.jsx` to "[not shadow the global "Map" property](https://eslint.org/docs/latest/rules/no-shadow)"
12+
- Create a mapper to map lats/longs to fit the outer bounds react-leaflet uses
13+
- Create a `MapMarker.jsx`
1014
- [PR-55](https://github.com/itk-dev/aapodwalk/pull/55)
1115
- Redesign pins on map
1216
- [PR-54](https://github.com/itk-dev/aapodwalk/pull/54)

src/components/map/Map.jsx

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { MapContainer, TileLayer } from "react-leaflet";
2+
import { useContext, useEffect, useState } from "react";
3+
import LatLongContext from "../../context/latitude-longitude-context";
4+
import YouAreHere from "../../icons/you-are-here-icon.svg?raw";
5+
import { getIndexedPinSvg, getPinSvg, mapArrayForOuterBounds } from "../../util/helper";
6+
import MapMarker from "./MapMarker";
7+
import "leaflet/dist/leaflet.css";
8+
import "./map-wrapper.css";
9+
10+
function MapComponent({ mapData, additionalClass = "", withIndex }) {
11+
const { lat, long } = useContext(LatLongContext);
12+
const [outerBounds, setOuterBounds] = useState(null);
13+
function getLabelForPin(index) {
14+
return index + 1;
15+
}
16+
17+
useEffect(() => {
18+
if (mapData.length > -1) {
19+
setOuterBounds(mapArrayForOuterBounds(mapData, lat, long));
20+
}
21+
}, [mapData, lat, long]);
22+
23+
if (outerBounds === null) return null;
24+
25+
return (
26+
<MapContainer bounds={outerBounds} className={`${additionalClass} rounded`} scrollWheelZoom={true}>
27+
<TileLayer
28+
attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
29+
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
30+
/>
31+
{mapData.map(({ latitude, longitude }, index) => (
32+
<MapMarker
33+
key={latitude}
34+
latitude={latitude}
35+
longitude={longitude}
36+
iconHtml={withIndex ? getIndexedPinSvg(getLabelForPin(index)) : getPinSvg()}
37+
/>
38+
))}
39+
{lat && long && <MapMarker latitude={lat} longitude={long} iconHtml={YouAreHere} />}
40+
</MapContainer>
41+
);
42+
}
43+
export default MapComponent;

src/components/map/MapMarker.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Marker } from "react-leaflet";
2+
import L from "leaflet";
3+
4+
const MapMarker = ({ iconHtml, latitude, longitude }) => {
5+
return (
6+
<Marker
7+
key={latitude}
8+
position={[latitude, longitude]}
9+
icon={L.divIcon({
10+
html: iconHtml,
11+
// The empty string classname below seems like something to remove, but if I remove it, a little square
12+
// appears in the map... Not sure why
13+
className: "",
14+
iconSize: [24, 24], // If icon size is changed, it should equally be changed in the map-wrapper.css
15+
iconAnchor: [24, 24], // 24 centers it, 24 makes the pointy end point on the right coordinates
16+
})}
17+
/>
18+
);
19+
};
20+
21+
export default MapMarker;

src/components/map/MapWrapper.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState, useContext } from "react";
2-
import Map from "./Map";
2+
import MapComponent from "./MapComponent";
33
import PermissionContext from "../../context/permission-context";
44
import "./map-wrapper.css";
55
import CloseButton from "../CloseButton";
@@ -20,9 +20,9 @@ function MapWrapper({ mapData, additionalClass = "", focusable, withIndex }) {
2020
}
2121
onClick={() => setFocusOnMap(true)}
2222
>
23-
{focusOnMap && <Map withIndex={withIndex} zoomControl mapData={mapData} />}
23+
{focusOnMap && <MapComponent withIndex={withIndex} mapData={mapData} />}
2424
{!focusOnMap && focusable && (
25-
<Map withIndex={withIndex} zoomControl={false} additionalClass={additionalClass} mapData={mapData} />
25+
<MapComponent withIndex={withIndex} additionalClass={additionalClass} mapData={mapData} />
2626
)}
2727
</button>
2828
{focusOnMap && focusable && (

src/components/points/DistanceComponent.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function DistanceComponent({ id, latitude, longitude, classes, proximityToUnlock
88
const { lat, long } = useContext(LatLongContext);
99
const distance = useMemo(
1010
() => getDistanceBetweenCoordinates(lat, long, latitude, longitude),
11-
[lat, long, latitude, longitude]
11+
[lat, long, latitude, longitude],
1212
);
1313
function unlockThisPoint() {
1414
setListOfUnlocked([...listOfUnlocked, ...[id]]);

src/util/helper.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,14 @@ export function getPinSvg() {
7777
</svg>`;
7878
}
7979

80+
export function mapArrayForOuterBounds(pointArray, userLat, userLong) {
81+
if (userLat && userLong) {
82+
return [
83+
...pointArray.map(({ latitude, longitude }) => [latitude, longitude]),
84+
...[[userLat.toString(), userLong.toString()]],
85+
];
86+
}
87+
return pointArray.map(({ latitude, longitude }) => [latitude, longitude]);
88+
}
89+
8090
export default {};

0 commit comments

Comments
 (0)