admin管理员组文章数量:1421190
I create app with redux saga and I have problem with geolocation. Actually I found the solution but I don't understand how it works.
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 } });
}
}
I understand getUserLocation but when it es to userPositionPromised I don't get it. Especially this part:
location => position.on({location}),
error => position.on({error}),
and
return { getLocation: () => new Promise(location => position.on = location) }
I create app with redux saga and I have problem with geolocation. Actually I found the solution but I don't understand how it works.
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 } });
}
}
I understand getUserLocation but when it es to userPositionPromised I don't get it. Especially this part:
location => position.on({location}),
error => position.on({error}),
and
return { getLocation: () => new Promise(location => position.on = location) }
Share
Improve this question
edited Sep 12, 2019 at 13:26
B--rian
5,89611 gold badges48 silver badges100 bronze badges
asked Mar 9, 2018 at 14:12
LukeLuke
5272 gold badges8 silver badges19 bronze badges
1 Answer
Reset to default 9I tried running the code above, I got an error here
location => position.on({location}),
error => position.on({error}),
What worked for me was to create a promise definition that resolves upon retrieving a location. For example:
const getUserLocation = () => new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
location => resolve(location),
error => reject(error),
)
})
then you simply call this from within the generator like any other service.
function* myGenerator() {
const location = yield call(getUserLocation)
const {latitude, longitude} = location.coords;
}
Enjoy
本文标签: javascriptRedux Saga navigatorgeolocationgetCurrentPositionStack Overflow
版权声明:本文标题:javascript - Redux Saga navigator.geolocation.getCurrentPosition - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745348930a2654643.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论