feat: check location according to radius
This commit is contained in:
parent
aeb1b32441
commit
cbe4ebc8d1
|
@ -14,7 +14,8 @@ import { toast } from "@/lib/utils"
|
||||||
// Update the elements according to distance remaining
|
// Update the elements according to distance remaining
|
||||||
function locationSuccessCallback(
|
function locationSuccessCallback(
|
||||||
position: GeolocationPosition,
|
position: GeolocationPosition,
|
||||||
targetPosition: LatLngTuple
|
targetPosition: LatLngTuple,
|
||||||
|
radius: number
|
||||||
) {
|
) {
|
||||||
// Enable current location control
|
// Enable current location control
|
||||||
removeClasses("current-location-control", "disabled-button")
|
removeClasses("current-location-control", "disabled-button")
|
||||||
|
@ -27,7 +28,7 @@ function locationSuccessCallback(
|
||||||
)
|
)
|
||||||
|
|
||||||
// If user has arrived to destination
|
// If user has arrived to destination
|
||||||
if (distance < 100) {
|
if (distance < radius) {
|
||||||
// Change the description texts
|
// Change the description texts
|
||||||
updateText("button-text", "İçeriği Göster")
|
updateText("button-text", "İçeriği Göster")
|
||||||
updateText("locked-content-description", "İçeriği görmek için butona bas!")
|
updateText("locked-content-description", "İçeriği görmek için butona bas!")
|
||||||
|
|
|
@ -45,13 +45,27 @@ function getTargetPosition() {
|
||||||
return data
|
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
|
// Call Geolocation API to start watching user location
|
||||||
function startWatchingLocation() {
|
function startWatchingLocation() {
|
||||||
const TARGET_POSITION = getTargetPosition()
|
const TARGET_POSITION = getTargetPosition()
|
||||||
|
const radius = getRadius()
|
||||||
|
|
||||||
if (!watchId) {
|
if (!watchId) {
|
||||||
watchId = window.navigator.geolocation.watchPosition(
|
watchId = window.navigator.geolocation.watchPosition(
|
||||||
(position) => locationSuccessCallback(position, TARGET_POSITION),
|
(position) => locationSuccessCallback(position, TARGET_POSITION, radius),
|
||||||
errorCallback
|
errorCallback
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -65,8 +79,10 @@ navigator.permissions
|
||||||
switch (permissionStatus.state) {
|
switch (permissionStatus.state) {
|
||||||
case "granted":
|
case "granted":
|
||||||
const TARGET_POSITION = getTargetPosition()
|
const TARGET_POSITION = getTargetPosition()
|
||||||
|
const radius = getRadius()
|
||||||
watchId = window.navigator.geolocation.watchPosition(
|
watchId = window.navigator.geolocation.watchPosition(
|
||||||
(position) => locationSuccessCallback(position, TARGET_POSITION),
|
(position) =>
|
||||||
|
locationSuccessCallback(position, TARGET_POSITION, radius),
|
||||||
errorCallback
|
errorCallback
|
||||||
)
|
)
|
||||||
break
|
break
|
||||||
|
|
Loading…
Reference in New Issue
Block a user