fix fack location complated
All checks were successful
Build & Deploy on Dev / build (push) Successful in 2m22s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 2m22s
This commit is contained in:
parent
859d74056a
commit
3ae6e6eeac
4 changed files with 287 additions and 115 deletions
|
|
@ -16,7 +16,32 @@ const mapElement = ref<HTMLElement | null>(null)
|
|||
const emit = defineEmits(['update:location', 'locationStatus', 'mockDetected'])
|
||||
const $q = useQuasar()
|
||||
|
||||
const { validateLocation, showMockWarning } = useLocationValidation()
|
||||
const { validateLocation } = useLocationValidation()
|
||||
const MOCK_CHECK_DELAY_MS = 800
|
||||
|
||||
function wait(ms: number) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
function getCurrentPositionAsync(options?: PositionOptions) {
|
||||
return new Promise<GeolocationPosition>((resolve, reject) => {
|
||||
navigator.geolocation.getCurrentPosition(resolve, reject, options)
|
||||
})
|
||||
}
|
||||
|
||||
async function getPositionForValidation(fallback: GeolocationPosition) {
|
||||
await wait(MOCK_CHECK_DELAY_MS)
|
||||
try {
|
||||
return await getCurrentPositionAsync({
|
||||
enableHighAccuracy: true,
|
||||
timeout: 10000,
|
||||
maximumAge: 0,
|
||||
})
|
||||
} catch (error) {
|
||||
// Keep the original reading if the second sampling fails.
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
|
||||
function updateLocation(latitude: number, longitude: number, namePOI: string) {
|
||||
// ส่ง event ไปยัง parent component เพื่ออัพเดทค่า props
|
||||
|
|
@ -24,6 +49,7 @@ function updateLocation(latitude: number, longitude: number, namePOI: string) {
|
|||
}
|
||||
|
||||
const poiPlaceName = ref<string>('') // ชื่อพื้นที่ใกล้เคียง
|
||||
const isMapReady = ref<boolean>(false)
|
||||
|
||||
// Replace ArcGIS api key
|
||||
const apiKey = ref<string>(
|
||||
|
|
@ -68,6 +94,7 @@ async function initializeMap(position: GeolocationPosition) {
|
|||
components: [], // Empty array to remove all default UI components
|
||||
},
|
||||
})
|
||||
isMapReady.value = true
|
||||
|
||||
// ตำแหน่งของผู้ใช้
|
||||
const userPoint = new Point({ longitude, latitude })
|
||||
|
|
@ -180,7 +207,9 @@ async function initializeMap(position: GeolocationPosition) {
|
|||
updateLocation(latitude, longitude, poiPlaceName.value)
|
||||
})
|
||||
.catch((error) => {
|
||||
// console.error('Error fetching points of interest:', error)
|
||||
// Keep map visible even when POI lookup fails.
|
||||
poiPlaceName.value = 'ไม่พบข้อมูล'
|
||||
updateLocation(latitude, longitude, poiPlaceName.value)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
@ -213,13 +242,16 @@ const requestLocationPermission = () => {
|
|||
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
async (position) => {
|
||||
// Validate location first
|
||||
const validationResult = validateLocation(position)
|
||||
const sampledPosition = await getPositionForValidation(position)
|
||||
|
||||
// Always emit mockDetected event (regardless of result)
|
||||
// Validate location first
|
||||
const validationResult = validateLocation(sampledPosition)
|
||||
|
||||
// Do not block map preview on initial mock detection.
|
||||
// The hard-stop warning is enforced at submit time in HomeView.
|
||||
if (validationResult.isMockDetected) {
|
||||
showMockWarning(validationResult)
|
||||
emit('mockDetected', validationResult)
|
||||
locationGranted.value = true
|
||||
emit('locationStatus', true)
|
||||
}
|
||||
|
||||
// Check for critical errors (invalid coordinates) that prevent showing location
|
||||
|
|
@ -234,11 +266,11 @@ const requestLocationPermission = () => {
|
|||
return
|
||||
}
|
||||
|
||||
// Permission granted based on mock detection
|
||||
locationGranted.value = !validationResult.isMockDetected
|
||||
emit('locationStatus', !validationResult.isMockDetected)
|
||||
// Permission granted for map preview state.
|
||||
locationGranted.value = true
|
||||
emit('locationStatus', true)
|
||||
|
||||
const { latitude, longitude } = position.coords
|
||||
const { latitude, longitude } = sampledPosition.coords
|
||||
// console.log('Current position:', latitude, longitude)
|
||||
|
||||
if (!latitude || !longitude) {
|
||||
|
|
@ -248,7 +280,7 @@ const requestLocationPermission = () => {
|
|||
|
||||
// Center map on user's location if map is initialized
|
||||
if (privacyStore.isAccepted) {
|
||||
await initializeMap(position)
|
||||
await initializeMap(sampledPosition)
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
|
|
@ -279,7 +311,7 @@ const requestLocationPermission = () => {
|
|||
break
|
||||
}
|
||||
},
|
||||
{ enableHighAccuracy: true }
|
||||
{ enableHighAccuracy: true, timeout: 10000, maximumAge: 0 }
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -291,7 +323,7 @@ defineExpose({
|
|||
|
||||
<template>
|
||||
<!-- Loading skeleton -->
|
||||
<div v-if="!poiPlaceName" class="col-12">
|
||||
<div v-if="!isMapReady" class="col-12">
|
||||
<q-skeleton
|
||||
:height="$q.screen.gt.xs ? '35vh' : '45px'"
|
||||
width="100%"
|
||||
|
|
@ -301,7 +333,7 @@ defineExpose({
|
|||
</div>
|
||||
|
||||
<q-card
|
||||
v-show="poiPlaceName"
|
||||
v-show="isMapReady"
|
||||
bordered
|
||||
flat
|
||||
class="col-12 bg-grey-2 shadow-0"
|
||||
|
|
@ -323,7 +355,7 @@ defineExpose({
|
|||
>
|
||||
พื้นที่ใกล้เคียง
|
||||
<span class="q-px-sm">:</span>
|
||||
{{ poiPlaceName }}
|
||||
{{ poiPlaceName || 'ไม่พบข้อมูล' }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
@ -340,7 +372,7 @@ defineExpose({
|
|||
</q-item-section>
|
||||
|
||||
<q-item-section>
|
||||
{{ poiPlaceName }}
|
||||
{{ poiPlaceName || 'ไม่พบข้อมูล' }}
|
||||
</q-item-section>
|
||||
</template>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue