engedelyek
This commit is contained in:
@@ -3,32 +3,41 @@ import { IconBack, IconPin, IconCompass, IconChevronRight, IconSun } from '../co
|
||||
import { latText, lonText } from '../lib/format'
|
||||
import { startCamera, startOrientation } from '../lib/camera'
|
||||
import { errText } from '../lib/wasm'
|
||||
import { EMBEDDED, nativeCall } from '../lib/files'
|
||||
import { t } from '../i18n'
|
||||
|
||||
const canLocate = () => EMBEDDED || !!navigator.geolocation
|
||||
const located = (latDeg, lonDeg, elevM, accuracyM) => ({
|
||||
status: 'ok', error: '', loc: { latDeg, lonDeg, elevM: typeof elevM === 'number' ? elevM : null, accuracyM },
|
||||
})
|
||||
const unavailable = () => ({ status: 'error', loc: null, error: t('setup.geoUnavailable') })
|
||||
|
||||
/** One fix → the hook's state ({status, loc, error}). */
|
||||
async function fetchLocation() {
|
||||
if (EMBEDDED) {
|
||||
// iOS shell: Core Location with the app's own permission (the WebView
|
||||
// would ask "localhost …" again on every launch). Android answers
|
||||
// {unsupported}: its WebView geolocation is already granted for good.
|
||||
const r = await nativeCall('getLocation')
|
||||
if (r && typeof r.lat === 'number') return located(r.lat, r.lon, r.alt, r.accuracy)
|
||||
if (r && r.error) return unavailable()
|
||||
}
|
||||
if (!navigator.geolocation) return { status: 'error', loc: null, error: t('setup.noGeo') }
|
||||
return new Promise((resolve) => navigator.geolocation.getCurrentPosition(
|
||||
(p) => resolve(located(p.coords.latitude, p.coords.longitude, p.coords.altitude, p.coords.accuracy)),
|
||||
() => resolve(unavailable()),
|
||||
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 60000 },
|
||||
))
|
||||
}
|
||||
|
||||
function useLocation() {
|
||||
const [state, setState] = useState(() => (navigator.geolocation
|
||||
const [state, setState] = useState(() => (canLocate()
|
||||
? { status: 'locating', loc: null, error: '' }
|
||||
: { status: 'error', loc: null, error: t('setup.noGeo') }))
|
||||
const locate = useCallback(() => {
|
||||
if (!navigator.geolocation) return
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
(p) => setState({
|
||||
status: 'ok',
|
||||
error: '',
|
||||
loc: {
|
||||
latDeg: p.coords.latitude,
|
||||
lonDeg: p.coords.longitude,
|
||||
elevM: p.coords.altitude,
|
||||
accuracyM: p.coords.accuracy,
|
||||
},
|
||||
}),
|
||||
() => setState({ status: 'error', loc: null, error: t('setup.geoUnavailable') }),
|
||||
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 60000 },
|
||||
)
|
||||
}, [])
|
||||
const locate = useCallback(() => { fetchLocation().then(setState) }, [])
|
||||
useEffect(() => { locate() }, [locate])
|
||||
const refresh = () => {
|
||||
if (!navigator.geolocation) return
|
||||
if (!canLocate()) return
|
||||
setState((s) => ({ ...s, status: 'locating', error: '' }))
|
||||
locate()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user