-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Zoom to and show user location (#37)
* fix: reenable board here and set destination stopmarker buttons * add experimental https to dev server * add LeafletControl wrapper * add UserLocation dot * fix: Location button position when footer visible * style: change setView to flyTo UserLocation * bump version to 0.9.0
- Loading branch information
1 parent
cc9cd4c
commit 3a12b30
Showing
7 changed files
with
147 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,5 @@ prisma/*.tgz | |
prisma/backup/* | ||
last_modified_log.txt | ||
prisma/*.db-journal | ||
|
||
certificates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import L from "leaflet"; | ||
import React, { ReactNode, useEffect, useRef } from "react"; | ||
|
||
const POSITION_CLASSES = { | ||
bottomleft: "leaflet-bottom leaflet-left", | ||
bottomright: "leaflet-bottom leaflet-right", | ||
topleft: "leaflet-top leaflet-left", | ||
topright: "leaflet-top leaflet-right", | ||
}; | ||
|
||
type ControlPosition = keyof typeof POSITION_CLASSES; | ||
interface LeafLetControlProps { | ||
position?: ControlPosition; | ||
children: ReactNode; | ||
} | ||
|
||
const LeafletControl: React.FC<LeafLetControlProps> = ({ | ||
position, | ||
children, | ||
}) => { | ||
const divRef = useRef(null); | ||
|
||
useEffect(() => { | ||
if (divRef.current) { | ||
L.DomEvent.disableClickPropagation(divRef.current); | ||
L.DomEvent.disableScrollPropagation(divRef.current); | ||
} | ||
}); | ||
|
||
return ( | ||
<div ref={divRef} className={position && POSITION_CLASSES[position]}> | ||
<div className={"leaflet-control"}>{children}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default LeafletControl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
import { useMap, CircleMarker, Circle, LayerGroup } from "react-leaflet"; | ||
import { Locate } from "lucide-react"; | ||
import LeafletControl from "./LeafletControl"; | ||
import { Button } from "../ui/button"; | ||
import { LocationEvent } from "leaflet"; | ||
import { cn } from "@/lib/utils"; | ||
|
||
type Props = { className?: string }; | ||
|
||
function UserLocation({ className = "" }: Props) { | ||
const map = useMap(); | ||
const [userLocation, setUserLocation] = useState<L.LatLng | null>(null); | ||
const [radius, setRadius] = useState(0); | ||
const prevLocation = useRef<L.LatLng | null>(); | ||
|
||
// Watch current user location | ||
useEffect(() => { | ||
const handleSetLocation = (e: LocationEvent) => { | ||
if ( | ||
!prevLocation.current || | ||
e.latlng.lat !== prevLocation.current.lat || | ||
e.latlng.lng !== prevLocation.current.lng | ||
) { | ||
prevLocation.current = userLocation; | ||
setUserLocation(e.latlng); | ||
setRadius(e.accuracy); | ||
} | ||
}; | ||
|
||
map | ||
.locate({ | ||
watch: true, | ||
enableHighAccuracy: true, | ||
maximumAge: 10_000, | ||
timeout: 15_000, | ||
}) | ||
.on("locationerror", (e) => console.error(e)) | ||
.on("locationfound", (e) => handleSetLocation(e)); | ||
|
||
return () => { | ||
map.stopLocate(); | ||
map.off("locationfound", handleSetLocation); | ||
map.off("locationerror"); | ||
}; | ||
}, [map, userLocation]); | ||
|
||
return ( | ||
<LeafletControl position={"bottomright"}> | ||
<Button | ||
onClick={() => { | ||
if (userLocation) { | ||
map.flyTo(userLocation); | ||
} | ||
}} | ||
size={"icon"} | ||
className={cn(className, "mb-4 mr-2 lg:mb-10 lg:mr-10")} | ||
> | ||
<Locate /> | ||
</Button> | ||
<LayerGroup> | ||
{!!userLocation && ( | ||
<> | ||
{radius > 100 && ( | ||
<Circle | ||
center={userLocation} | ||
radius={radius} | ||
stroke={false} | ||
fillOpacity={0.2} | ||
/> | ||
)} | ||
<CircleMarker | ||
center={userLocation} | ||
radius={6} | ||
color="white" | ||
stroke | ||
weight={2} | ||
fillColor="#1869E5" | ||
fillOpacity={1} | ||
/> | ||
</> | ||
)} | ||
</LayerGroup> | ||
</LeafletControl> | ||
); | ||
} | ||
|
||
export default UserLocation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters