feat: check location according to radius

This commit is contained in:
log101 2024-08-06 17:04:34 +03:00
parent aeb1b32441
commit cbe4ebc8d1
2 changed files with 21 additions and 4 deletions

View File

@ -14,7 +14,8 @@ import { toast } from "@/lib/utils"
// Update the elements according to distance remaining
function locationSuccessCallback(
position: GeolocationPosition,
targetPosition: LatLngTuple
targetPosition: LatLngTuple,
radius: number
) {
// Enable current location control
removeClasses("current-location-control", "disabled-button")
@ -27,7 +28,7 @@ function locationSuccessCallback(
)
// If user has arrived to destination
if (distance < 100) {
if (distance < radius) {
// Change the description texts
updateText("button-text", "İçeriği Göster")
updateText("locked-content-description", "İçeriği görmek için butona bas!")

View File

@ -45,13 +45,27 @@ function getTargetPosition() {
return data
}
function getRadius() {
const leafletMap = document.getElementById("map")
let targetRadiusString = leafletMap?.dataset.targetRadius
// TARGET_POSITION is required to calculate distance
if (!targetRadiusString) targetRadiusString = "50"
const data = Number(targetRadiusString)
return data
}
// Call Geolocation API to start watching user location
function startWatchingLocation() {
const TARGET_POSITION = getTargetPosition()
const radius = getRadius()
if (!watchId) {
watchId = window.navigator.geolocation.watchPosition(
(position) => locationSuccessCallback(position, TARGET_POSITION),
(position) => locationSuccessCallback(position, TARGET_POSITION, radius),
errorCallback
)
}
@ -65,8 +79,10 @@ navigator.permissions
switch (permissionStatus.state) {
case "granted":
const TARGET_POSITION = getTargetPosition()
const radius = getRadius()
watchId = window.navigator.geolocation.watchPosition(
(position) => locationSuccessCallback(position, TARGET_POSITION),
(position) =>
locationSuccessCallback(position, TARGET_POSITION, radius),
errorCallback
)
break