diff --git a/src/components/LockedContent/geolocation.ts b/src/components/LockedContent/geolocation.ts index 6030648..73efd46 100644 --- a/src/components/LockedContent/geolocation.ts +++ b/src/components/LockedContent/geolocation.ts @@ -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!") diff --git a/src/scripts/lockedContent.ts b/src/scripts/lockedContent.ts index 67db49c..7b69eee 100644 --- a/src/scripts/lockedContent.ts +++ b/src/scripts/lockedContent.ts @@ -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