forked from dcloudio/uni-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(location update): 修复回调及监听事件触发错误并调整默认坐标系为gcj02
- Loading branch information
1 parent
23f1c94
commit 8c312f0
Showing
2 changed files
with
98 additions
and
111 deletions.
There are no files selected for viewing
85 changes: 40 additions & 45 deletions
85
src/platforms/app-plus/service/api/location/location-change.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,75 @@ | ||
import { invoke } from '../../bridge' | ||
const callbackIds = [] | ||
const callbackOnErrorIds = [] | ||
const callbackOffErrorIds = [] | ||
let watchId | ||
|
||
/** | ||
* 开始更新定位 | ||
*/ | ||
export function startLocationUpdate ({ type = 'wgs84' }) { | ||
watchId = plus.geolocation.watchPosition( | ||
let successCallbackIds = [] | ||
let errorCallbackIds = [] | ||
let started = false | ||
let watchId = 0 | ||
|
||
export function startLocationUpdate ({ type = 'gcj02' }, callbackId) { | ||
watchId = watchId || plus.geolocation.watchPosition( | ||
res => { | ||
callbackIds.forEach(callbackId => { | ||
started = true | ||
successCallbackIds.forEach(callbackId => { | ||
invoke(callbackId, res.coords) | ||
}) | ||
}, | ||
error => { | ||
callbackOnErrorIds.forEach(callbackId => { | ||
if (!started) { | ||
invoke(callbackId, { errMsg: `startLocationUpdate:fail ${error.message}` }) | ||
started = true | ||
} | ||
errorCallbackIds.forEach(callbackId => { | ||
invoke(callbackId, { | ||
errMsg: 'onLocationChange:fail' + error.message | ||
errMsg: `onLocationChange:fail ${error.message}` | ||
}) | ||
}) | ||
}, | ||
{ | ||
coordsType: type | ||
} | ||
) | ||
setTimeout(() => { | ||
invoke(callbackId, { | ||
errMsg: 'startLocationUpdate:ok' | ||
}) | ||
}, 100) | ||
} | ||
|
||
/** | ||
* 暂停更新定位 | ||
* @param {*} callbackId | ||
*/ | ||
export function stopLocationUpdate (callbackId) { | ||
if (watchId) { | ||
export function stopLocationUpdate () { | ||
if (watchId !== 0) { | ||
plus.geolocation.clearWatch(watchId) | ||
} else { | ||
invoke(callbackId, { errMsg: 'stopLocationUpdate:fail' }) | ||
started = false | ||
watchId = 0 | ||
} | ||
return {} | ||
} | ||
|
||
/** | ||
* 监听更新定位 | ||
* @param {*} callbackId | ||
*/ | ||
export function onLocationChange (callbackId) { | ||
callbackIds.push(callbackId) | ||
} | ||
|
||
/** | ||
* 监听更新定位失败 | ||
* @param {*} callbackId | ||
*/ | ||
export function onLocationChangeError (callbackId) { | ||
callbackOnErrorIds.push(callbackId) | ||
successCallbackIds.push(callbackId) | ||
} | ||
|
||
// 移除实时地理位置变化事件的监听函数 | ||
export function offLocationChange (callbackId) { | ||
if (callbackId) { | ||
const index = callbackIds.indexOf(callbackId) | ||
const index = successCallbackIds.indexOf(callbackId) | ||
if (index >= 0) { | ||
callbackIds.splice(index, 1) | ||
} else { | ||
callbackOffErrorIds.forEach(callbackId => { | ||
invoke(callbackId, { | ||
errMsg: 'offLocationChange:fail' | ||
}) | ||
}) | ||
successCallbackIds.splice(index, 1) | ||
} | ||
} else { | ||
callbackIds.length = 0 | ||
successCallbackIds = [] | ||
} | ||
} | ||
|
||
// 移除实时地理位置变化事件的监听函数 | ||
export function onLocationChangeError (callbackId) { | ||
errorCallbackIds.push(callbackId) | ||
} | ||
|
||
export function offLocationChangeError (callbackId) { | ||
callbackOffErrorIds.push(callbackId) | ||
if (callbackId) { | ||
const index = errorCallbackIds.indexOf(callbackId) | ||
if (index >= 0) { | ||
errorCallbackIds.splice(index, 1) | ||
} | ||
} else { | ||
errorCallbackIds = [] | ||
} | ||
} |
124 changes: 58 additions & 66 deletions
124
src/platforms/h5/service/api/location/location-change.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,88 @@ | ||
import { translateGeo } from '../../../helpers/location' | ||
|
||
const { invokeCallbackHandler: invoke } = UniServiceJSBridge | ||
const callbackIds = [] | ||
const callbackOnErrorIds = [] | ||
const callbackOffErrorIds = [] | ||
let watchId | ||
let successCallbackIds = [] | ||
let errorCallbackIds = [] | ||
let started = false | ||
let watchId = 0 | ||
|
||
/** | ||
* 开始更新定位 | ||
*/ | ||
export function startLocationUpdate ({ type = 'wgs84' }) { | ||
if (navigator.geolocation) { | ||
watchId = navigator.geolocation.watchPosition( | ||
res => { | ||
translateGeo(type, res.coords) | ||
.then((coords) => { | ||
callbackIds.forEach(callbackId => { | ||
invoke(callbackId, coords) | ||
}) | ||
}).catch(error => { | ||
callbackOnErrorIds.forEach(callbackId => { | ||
invoke(callbackId, { | ||
errMsg: 'onLocationChange:fail' + error.message | ||
}) | ||
}) | ||
export function startLocationUpdate ({ type = 'gcj02' }, callbackId) { | ||
if (!navigator.geolocation) { | ||
return { | ||
errMsg: 'startLocationUpdate:fail' | ||
} | ||
} | ||
|
||
watchId = watchId || navigator.geolocation.watchPosition( | ||
res => { | ||
started = true | ||
translateGeo(type, res.coords) | ||
.then((coords) => { | ||
successCallbackIds.forEach(callbackId => { | ||
invoke(callbackId, coords) | ||
}) | ||
}, | ||
error => { | ||
callbackOnErrorIds.forEach(callbackId => { | ||
invoke(callbackId, { | ||
errMsg: 'onLocationChange:fail' + error.message | ||
}).catch(error => { | ||
errorCallbackIds.forEach(callbackId => { | ||
invoke(callbackId, { | ||
errMsg: `onLocationChange:fail ${error.message}` | ||
}) | ||
}) | ||
}) | ||
}, | ||
error => { | ||
if (!started) { | ||
invoke(callbackId, { errMsg: `startLocationUpdate:fail ${error.message}` }) | ||
started = true | ||
} | ||
) | ||
} else { | ||
callbackOnErrorIds.forEach(callbackId => { | ||
invoke(callbackId, { | ||
errMsg: 'onLocationChange:fail device nonsupport geolocation' | ||
errorCallbackIds.forEach(callbackId => { | ||
invoke(callbackId, { | ||
errMsg: `onLocationChange:fail ${error.message}` | ||
}) | ||
}) | ||
} | ||
) | ||
setTimeout(() => { | ||
invoke(callbackId, { | ||
errMsg: 'startLocationUpdate:ok' | ||
}) | ||
} | ||
}, 100) | ||
} | ||
|
||
/** | ||
* 暂停更新定位 | ||
* @param {*} callbackId | ||
*/ | ||
export function stopLocationUpdate (callbackId) { | ||
if (watchId) { | ||
export function stopLocationUpdate () { | ||
if (watchId !== 0) { | ||
navigator.geolocation.clearWatch(watchId) | ||
} else { | ||
invoke(callbackId, { errMsg: 'stopLocationUpdate:fail' }) | ||
started = false | ||
watchId = 0 | ||
} | ||
return {} | ||
} | ||
|
||
/** | ||
* 监听更新定位 | ||
* @param {*} callbackId | ||
*/ | ||
export function onLocationChange (callbackId) { | ||
callbackIds.push(callbackId) | ||
} | ||
|
||
/** | ||
* 监听更新定位失败 | ||
* @param {*} callbackId | ||
*/ | ||
export function onLocationChangeError (callbackId) { | ||
callbackOnErrorIds.push(callbackId) | ||
successCallbackIds.push(callbackId) | ||
} | ||
|
||
// 移除实时地理位置变化事件的监听函数 | ||
export function offLocationChange (callbackId) { | ||
if (callbackId) { | ||
const index = callbackIds.indexOf(callbackId) | ||
const index = successCallbackIds.indexOf(callbackId) | ||
if (index >= 0) { | ||
callbackIds.splice(index, 1) | ||
} else { | ||
callbackOffErrorIds.forEach(callbackId => { | ||
invoke(callbackId, { | ||
errMsg: 'offLocationChange:fail' | ||
}) | ||
}) | ||
successCallbackIds.splice(index, 1) | ||
} | ||
} else { | ||
callbackIds.length = 0 | ||
successCallbackIds = [] | ||
} | ||
} | ||
|
||
// 移除实时地理位置变化事件的监听函数 | ||
export function onLocationChangeError (callbackId) { | ||
errorCallbackIds.push(callbackId) | ||
} | ||
|
||
export function offLocationChangeError (callbackId) { | ||
callbackOffErrorIds.push(callbackId) | ||
if (callbackId) { | ||
const index = errorCallbackIds.indexOf(callbackId) | ||
if (index >= 0) { | ||
errorCallbackIds.splice(index, 1) | ||
} | ||
} else { | ||
errorCallbackIds = [] | ||
} | ||
} |