| | |
| | | let locationMap: any |
| | | let locationMarker: any |
| | | let amapApi: any |
| | | let browserLocationRequest: Promise<{ longitude: number; latitude: number } | undefined> | undefined |
| | | |
| | | async function loadAMap() { |
| | | if (amapApi) return amapApi |
| | |
| | | activeFuelStore.value = row |
| | | pendingLongitude.value = String(row.longitude || '') |
| | | pendingLatitude.value = String(row.latitude || '') |
| | | browserLocationRequest = selectedCoordinates() ? undefined : requestBrowserLocation() |
| | | locationDialogVisible.value = true |
| | | locationSearch.value = row.address || '' |
| | | await initializeLocationMap() |
| | |
| | | activeParkingAddress.value = row |
| | | pendingLongitude.value = String(row.longitude || '') |
| | | pendingLatitude.value = String(row.latitude || '') |
| | | browserLocationRequest = selectedCoordinates() ? undefined : requestBrowserLocation() |
| | | locationDialogVisible.value = true |
| | | locationSearch.value = row.address || '' |
| | | await initializeLocationMap() |
| | |
| | | activeSchoolAddress.value = editingSchool |
| | | pendingLongitude.value = String(editingSchool.longitude || '') |
| | | pendingLatitude.value = String(editingSchool.latitude || '') |
| | | browserLocationRequest = selectedCoordinates() ? undefined : requestBrowserLocation() |
| | | locationDialogVisible.value = true |
| | | locationSearch.value = editingSchool.address || '' |
| | | await initializeLocationMap() |
| | |
| | | if (!longitudeText || !latitudeText) return undefined |
| | | const longitude = Number(longitudeText) |
| | | const latitude = Number(latitudeText) |
| | | // The API uses zero for an unset coordinate. Treat it as empty instead of |
| | | // opening the map at 0,0. |
| | | if (longitude === 0 || latitude === 0) return undefined |
| | | return Number.isFinite(longitude) && Number.isFinite(latitude) ? { longitude, latitude } : undefined |
| | | } |
| | | |
| | | function requestBrowserLocation(): Promise<{ longitude: number; latitude: number } | undefined> { |
| | | return new Promise((resolve) => { |
| | | const geolocation = window.navigator?.geolocation |
| | | if (!geolocation) { |
| | | ElMessage.info('当前浏览器不支持定位,请直接搜索或点击地图标记') |
| | | resolve(undefined) |
| | | return |
| | | } |
| | | const hostname = window.location.hostname |
| | | if (!window.isSecureContext && hostname !== 'localhost' && hostname !== '127.0.0.1') { |
| | | ElMessage.warning('浏览器定位需要使用 localhost 或 HTTPS 地址访问') |
| | | resolve(undefined) |
| | | return |
| | | } |
| | | geolocation.getCurrentPosition( |
| | | (position) => { |
| | | const longitude = position.coords.longitude |
| | | const latitude = position.coords.latitude |
| | | resolve(Number.isFinite(longitude) && Number.isFinite(latitude) ? { longitude, latitude } : undefined) |
| | | }, |
| | | (error) => { |
| | | if (error.code === error.PERMISSION_DENIED) ElMessage.info('未获得浏览器定位权限,请直接搜索或点击地图标记') |
| | | resolve(undefined) |
| | | }, |
| | | { enableHighAccuracy: true, timeout: 10000, maximumAge: 0 }, |
| | | ) |
| | | }) |
| | | } |
| | | |
| | | function setMapMarker(latitude: number, longitude: number, center = true) { |
| | |
| | | reverseGeocodeLocation(latitude, longitude) |
| | | }) |
| | | if (selected) setMapMarker(initial.latitude, initial.longitude, false) |
| | | if (!selected) { |
| | | const browserPosition = await (browserLocationRequest || requestBrowserLocation()) |
| | | browserLocationRequest = undefined |
| | | if (browserPosition && locationMap && !locationMarker && !selectedCoordinates()) { |
| | | setMapMarker(browserPosition.latitude, browserPosition.longitude) |
| | | } |
| | | } |
| | | window.setTimeout(() => locationMap?.resize(), 120) |
| | | } catch (error) { |
| | | console.error('Failed to initialize AMap', error) |
| | |
| | | locationMap?.destroy() |
| | | locationMap = undefined |
| | | locationMarker = undefined |
| | | browserLocationRequest = undefined |
| | | locationSearchResults.value = [] |
| | | } |
| | | |
| | |
| | | activeFuelStore.value = undefined |
| | | pendingLongitude.value = String(form.longitude || '') |
| | | pendingLatitude.value = String(form.latitude || '') |
| | | browserLocationRequest = selectedCoordinates() ? undefined : requestBrowserLocation() |
| | | locationDialogVisible.value = true |
| | | locationSearch.value = form.address || '' |
| | | await initializeLocationMap() |