【发布时间】:2023-11-07 19:04:01
【问题描述】:
我使用 redux saga 创建应用程序,但我遇到了地理定位问题。 实际上我找到了解决方案,但我不明白它是如何工作的。
function userPositionPromised() {
const position = {}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition (
location => position.on({location}),
error => position.on({error}),
{ enableHighAccuracy: true }
)
}
return { getLocation: () => new Promise(location => position.on = location) }
}
function* getUserLocation() {
yield put({type: GET_LOCATION_REQUESTED});
const { getLocation } = yield call(userPositionPromised)
const { error, location } = yield call(getLocation)
if (error) {
console.log('Failed to get user position!', error)
const { message, code } = error;
yield put({type: GET_LOCATION_FAILED, payload: { code, message }});
} else {
console.log('Received User Location', location)
const { latitude: lat, longitude: lng } = location.coords;
yield put({type: GET_LOCATION_SUCCESS, payload: { lat, lng } });
}
}
我了解 getUserLocation 但是当涉及到 userPositionPromised 我不明白。尤其是这部分:
location => position.on({location}),
error => position.on({error}),
和
return { getLocation: () => new Promise(location => position.on = location) }
【问题讨论】:
标签: javascript reactjs geolocation redux-saga