Skip to content

Commit

Permalink
More maps
Browse files Browse the repository at this point in the history
  • Loading branch information
OlliV committed Oct 26, 2023
1 parent d4e855b commit 8860b40
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 84 deletions.
6 changes: 3 additions & 3 deletions components/EditRideModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export default function EditModal({
onChange={handleNameChange}
sx={{
width: '40ch',
pb: '2.5em'
pb: '2.5em',
}}
/>
<br/>
<br />
<TextField
id="act-notes"
label="Notes"
Expand All @@ -70,7 +70,7 @@ export default function EditModal({
variant="outlined"
fullWidth
sx={{
pb: '2.5em'
pb: '2.5em',
}}
/>
<EditActionButtons onClickSave={onClickSave} onClickDiscard={onClickDiscard} />
Expand Down
38 changes: 38 additions & 0 deletions components/MapMarker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use client';
import L from 'leaflet';
import { Marker, Popup } from 'react-leaflet';
import { useEffect } from 'react';
import MarkerIcon from '../node_modules/leaflet/dist/images/marker-icon.png';
import MarkerShadow from '../node_modules/leaflet/dist/images/marker-shadow.png';

export default function MapMarker({ map, position }) {
useEffect(() => {
if (map) {
console.log('fly to ', position);
map.flyTo(position, map.getZoom());
}
}, [map, position]);

return (
<Marker
position={position}
// @ts-ignore
icon={
new L.Icon({
iconUrl: MarkerIcon.src,
iconRetinaUrl: MarkerIcon.src,
iconSize: [25, 41],
iconAnchor: [12.5, 41],
popupAnchor: [0, -41],
shadowUrl: MarkerShadow.src,
shadowSize: [41, 41],
})
}
>
<Popup>
You are here.
{`${position}`}
</Popup>
</Marker>
);
}
109 changes: 32 additions & 77 deletions components/OpenStreetMap.tsx
Original file line number Diff line number Diff line change
@@ -1,78 +1,33 @@
'use client'

import L from 'leaflet'
import MarkerIcon from '../node_modules/leaflet/dist/images/marker-icon.png'
import MarkerShadow from '../node_modules/leaflet/dist/images/marker-shadow.png'
import 'leaflet/dist/leaflet.css'
import { MapContainer, TileLayer, Marker, Popup, useMap } from 'react-leaflet'
import { useEffect, useState } from 'react'
'use client';
import 'leaflet/dist/leaflet.css';
import { MapContainer, TileLayer } from 'react-leaflet';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button'
import Stack from '@mui/material/Stack'

function MyLocationButton({ setPosition }) {
const getMyLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
setPosition([position.coords.latitude, position.coords.longitude])
})
} else {
console.log("Geolocation is not supported by this browser.")
}
};

return (
<Button variant="contained" onClick={getMyLocation}>Get My Location</Button>
)
}

function StartMarker({pos}) {
const map = useMap();

useEffect(() => {
map.flyTo(pos, map.getZoom());
}, [map, pos]);

return (<Marker icon={
new L.Icon({
iconUrl: MarkerIcon.src,
iconRetinaUrl: MarkerIcon.src,
iconSize: [25, 41],
iconAnchor: [12.5, 41],
popupAnchor: [0, -41],
shadowUrl: MarkerShadow.src,
shadowSize: [41, 41],
})
} position={pos}>
<Popup>
You are here.
</Popup>
</Marker>);
}

const OpenStreetMap = () => {

const [coord, setCoord] = useState([51.505, -0.09])

return (
<Box>
<Stack spacing={2} direction="row">
<MyLocationButton setPosition={setCoord} />
<Button variant="contained">Load GPX</Button>
</Stack>
<MapContainer style={{
height: '70vh',
width: '70vw'
}} center={coord} zoom={13} scrollWheelZoom={false}>
<TileLayer
attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>

<StartMarker pos={coord} />
</MapContainer>
</Box>
)
}

export default OpenStreetMap
import { ReactNode } from 'react';

const OpenStreetMap = ({ children, center, setMap }: { children?: ReactNode; center: number[]; setMap: any }) => {
return (
<Box>
<MapContainer
style={{
height: '70vh',
width: '70vw',
}}
// @ts-ignore
center={center}
zoom={13}
scrollWheelZoom={false}
ref={setMap}
>
<TileLayer
// @ts-ignore
attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>

{children}
</MapContainer>
</Box>
);
};

export default OpenStreetMap;
53 changes: 49 additions & 4 deletions pages/ride/map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,67 @@ import dynamic from 'next/dynamic';
import Box from '@mui/material/Box';
import Container from '@mui/material/Container';
import Grid from '@mui/material/Grid';
import Stack from '@mui/material/Stack';
import Button from '@mui/material/Button';
import { useState } from 'react';
import MyHead from '../../../components/MyHead';
import Title from '../../../components/Title';
import OpenStreetMap from '../../../components/OpenStreetMap';
import MapMarker from '../../../components/MapMarker';

const DynamicMap = dynamic(() => import('../../../components/OpenStreetMap'), {
ssr: false
type OpenStreetMapArg = Parameters<typeof OpenStreetMap>[0];
type MapMarkerArg = Parameters<typeof MapMarker>[0];

const DynamicMap = dynamic<OpenStreetMapArg>(() => import('../../../components/OpenStreetMap'), {
ssr: false,
});
const DynamicMapMarker = dynamic<MapMarkerArg>(() => import('../../../components/MapMarker'), {
ssr: false,
});

export default function RideWorkout() {
function MyLocationButton({ setPosition }) {
const getMyLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
setPosition([position.coords.latitude, position.coords.longitude]);
});
} else {
console.log('Geolocation is not supported by this browser.');
}
};

return (
<Button variant="contained" onClick={getMyLocation}>
Get My Location
</Button>
);
}

export default function RideMap() {
const [map, setMap] = useState(null);
const [coord, setCoord] = useState([51.505, -0.09]);

return (
<Container maxWidth="md">
<MyHead title="Map Ride" />
<Box>
<Title href="/ride">Map Ride</Title>
<p>Plan your ride.</p>

<DynamicMap />
<Stack
spacing={2}
direction="row"
sx={{
pb: '1ex',
}}
>
<MyLocationButton setPosition={setCoord} />
<Button variant="contained">Load GPX</Button>
</Stack>

<DynamicMap center={coord} setMap={setMap}>
<DynamicMapMarker map={map} position={coord} />
</DynamicMap>

<Grid container direction="row" alignItems="center" spacing={2}></Grid>
</Box>
Expand Down

1 comment on commit 8860b40

@vercel
Copy link

@vercel vercel bot commented on 8860b40 Oct 26, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

bfree – ./

bfree-git-master-olliv.vercel.app
bfree.vercel.app
bfree-olliv.vercel.app

Please sign in to comment.