-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
2,216 additions
and
1,363 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,44 @@ | ||
import { Box, SxProps, Theme } from "@mui/material"; | ||
import { Explore as ExploreIcon } from "@mui/icons-material"; | ||
import { isSafari, requestPermission } from "react-world-compass"; | ||
import { useCallback, useContext } from "react"; | ||
import AppContext from "../../AppContext"; | ||
|
||
const CompassControl = () => { | ||
const { compassPermission, setCompassPermission } = useContext(AppContext); | ||
const handleClick = useCallback(() => { | ||
requestPermission().then((r) => { | ||
// @ts-ignore | ||
setCompassPermission(r); | ||
}); | ||
}, [setCompassPermission]); | ||
|
||
if (!isSafari || compassPermission === "granted") { | ||
return <></>; | ||
} | ||
|
||
return ( | ||
<div className="leaflet-bottom leaflet-right"> | ||
<Box | ||
sx={compassControlSx} | ||
className="leaflet-control leaflet-bar" | ||
onClick={handleClick} | ||
> | ||
<ExploreIcon sx={{ p: "3px", color: "black" }} /> | ||
</Box> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CompassControl; | ||
|
||
const compassControlSx: SxProps<Theme> = { | ||
background: "white", | ||
width: 32, | ||
height: 32, | ||
marginBottom: "57px !important", | ||
marginRight: "5px !important", | ||
display: "flex", | ||
justifyContent: "center", | ||
alignItems: "center", | ||
}; |
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,105 @@ | ||
import { | ||
forwardRef, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
import AppContext from "../../AppContext"; | ||
import { checkPosition } from "../../utils"; | ||
import { Circle, Marker } from "react-leaflet"; | ||
import L from "leaflet"; | ||
import useCompass, { OrientationState } from "react-world-compass"; | ||
import "leaflet-rotatedmarker"; | ||
|
||
interface RotatedMarkerType<T> extends L.Marker<T> { | ||
setRotationAngle: (angle: number) => void; | ||
setRotationOrigin: (origin: string) => void; | ||
} | ||
|
||
const RotatedMarker = forwardRef<RotatedMarkerType<any>, any>( | ||
({ children, ...props }, forwardRef) => { | ||
const markerRef = useRef<RotatedMarkerType<any>>(null); | ||
|
||
const _compass = useCompass(100); | ||
const [compass, setCompass] = useState<OrientationState | null>(null); | ||
useEffect(() => { | ||
const elf = (nativeEvent: any) => { | ||
try { | ||
const data = JSON.parse(nativeEvent.data); | ||
if (data?.type === "compass") { | ||
setCompass(data.compass); | ||
} | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}; | ||
window.addEventListener("message", elf); | ||
return () => { | ||
window.removeEventListener("message", elf); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
setCompass(_compass); | ||
}, [_compass]); | ||
|
||
useEffect(() => { | ||
const marker = markerRef.current; | ||
if (marker && compass) { | ||
marker.setRotationAngle(360 - compass.degree); | ||
marker.setRotationOrigin("center"); | ||
} | ||
}, [compass]); | ||
|
||
if (compass === null) { | ||
return <></>; | ||
} | ||
|
||
return ( | ||
<Marker | ||
ref={(ref) => { | ||
markerRef.current = ref as RotatedMarkerType<any>; | ||
if (forwardRef) { | ||
// @ts-ignore | ||
forwardRef.current = ref as RotatedMarkerType<any>; | ||
} | ||
}} | ||
{...props} | ||
> | ||
{children} | ||
</Marker> | ||
); | ||
} | ||
); | ||
|
||
const SelfCircle = () => { | ||
const { geolocation, geoPermission } = useContext(AppContext); | ||
const icon = useMemo(() => myIcon(), []); | ||
|
||
if (geoPermission !== "granted") { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<Circle center={checkPosition(geolocation)} radius={25} /> | ||
<RotatedMarker | ||
rotationOrigin="center" | ||
icon={icon} | ||
position={checkPosition(geolocation)} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default SelfCircle; | ||
|
||
const myIcon = () => { | ||
return L.divIcon({ | ||
iconSize: [20, 20], | ||
iconAnchor: [10, 10], | ||
className: "self-center", | ||
}); | ||
}; |
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
Oops, something went wrong.