Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
8c312f00
U
uni-app
项目概览
DCloud
/
uni-app
3 个月 前同步成功
通知
718
Star
38705
Fork
3642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
7
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
7
Issue
7
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8c312f00
编写于
10月 19, 2022
作者:
DCloud-WZF
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(location update): 修复回调及监听事件触发错误并调整默认坐标系为gcj02
上级
23f1c944
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
98 addition
and
111 deletion
+98
-111
src/platforms/app-plus/service/api/location/location-change.js
...latforms/app-plus/service/api/location/location-change.js
+40
-45
src/platforms/h5/service/api/location/location-change.js
src/platforms/h5/service/api/location/location-change.js
+58
-66
未找到文件。
src/platforms/app-plus/service/api/location/location-change.js
浏览文件 @
8c312f00
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
}
`
})
})
},
...
...
@@ -25,56 +28,48 @@ export function startLocationUpdate ({ type = 'wgs84' }) {
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
)
{
c
allbackIds
.
push
(
callbackId
)
successC
allbackIds
.
push
(
callbackId
)
}
/**
* 监听更新定位失败
* @param {*} callbackId
*/
export
function
onLocationChangeError
(
callbackId
)
{
callbackOnErrorIds
.
push
(
callbackId
)
}
// 移除实时地理位置变化事件的监听函数
export
function
offLocationChange
(
callbackId
)
{
if
(
callbackId
)
{
const
index
=
c
allbackIds
.
indexOf
(
callbackId
)
const
index
=
successC
allbackIds
.
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
=
[]
}
}
src/platforms/h5/service/api/location/location-change.js
浏览文件 @
8c312f00
import
{
translateGeo
}
from
'
../../../helpers/location
'
const
{
invokeCallbackHandler
:
invoke
}
=
UniServiceJSBridge
const
c
allbackIds
=
[]
const
callbackOnError
Ids
=
[]
const
callbackOffErrorIds
=
[]
let
watchId
let
successC
allbackIds
=
[]
let
errorCallback
Ids
=
[]
let
started
=
false
let
watchId
=
0
/**
* 开始更新定位
*/
export
function
startLocationUpdate
({
type
=
'
wgs84
'
})
{
if
(
navigator
.
geolocation
)
{
watchId
=
navigator
.
geolocation
.
watchPosition
(
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
)
=>
{
c
allbackIds
.
forEach
(
callbackId
=>
{
successC
allbackIds
.
forEach
(
callbackId
=>
{
invoke
(
callbackId
,
coords
)
})
}).
catch
(
error
=>
{
callbackOnError
Ids
.
forEach
(
callbackId
=>
{
errorCallback
Ids
.
forEach
(
callbackId
=>
{
invoke
(
callbackId
,
{
errMsg
:
'
onLocationChange:fail
'
+
error
.
message
errMsg
:
`onLocationChange:fail
${
error
.
message
}
`
})
})
})
},
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
}
`
})
})
}
)
}
else
{
callbackOnErrorIds
.
forEach
(
callbackId
=>
{
setTimeout
(()
=>
{
invoke
(
callbackId
,
{
errMsg
:
'
onLocationChange:fail device nonsupport geolocation
'
})
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
=
c
allbackIds
.
indexOf
(
callbackId
)
const
index
=
successC
allbackIds
.
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
=
[]
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录