Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
e2c6b14e
U
uni-app
项目概览
DCloud
/
uni-app
4 个月 前同步成功
通知
730
Star
38706
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看板
提交
e2c6b14e
编写于
10月 28, 2020
作者:
Q
qiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: uni.offCompassChange
上级
6ced4d4d
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
118 addition
and
127 deletion
+118
-127
lib/apis.js
lib/apis.js
+1
-0
src/core/service/api/device/compass.js
src/core/service/api/device/compass.js
+0
-48
src/platforms/app-plus/service/api/device/compass.js
src/platforms/app-plus/service/api/device/compass.js
+46
-40
src/platforms/h5/service/api/device/compass.js
src/platforms/h5/service/api/device/compass.js
+57
-26
src/platforms/h5/view/components/map/index.vue
src/platforms/h5/view/components/map/index.vue
+14
-13
未找到文件。
lib/apis.js
浏览文件 @
e2c6b14e
...
...
@@ -77,6 +77,7 @@ const device = [
'
startAccelerometer
'
,
'
stopAccelerometer
'
,
'
onCompassChange
'
,
'
offCompassChange
'
,
'
startCompass
'
,
'
stopCompass
'
,
'
onGyroscopeChange
'
,
...
...
src/core/service/api/device/compass.js
已删除
100644 → 0
浏览文件 @
6ced4d4d
import
{
invoke
}
from
'
uni-core/service/bridge
'
import
{
onMethod
,
invokeMethod
}
from
'
../../platform
'
const
callbacks
=
[]
onMethod
(
'
onCompassChange
'
,
function
(
res
)
{
callbacks
.
forEach
(
callbackId
=>
{
invoke
(
callbackId
,
res
)
})
})
let
isEnable
=
false
/**
* 监听加速度
* @param {*} callbackId
*/
export
function
onCompassChange
(
callbackId
)
{
// TODO 当没有 start 时,添加 on 需要主动 start?
callbacks
.
push
(
callbackId
)
if
(
!
isEnable
)
{
startCompass
()
}
}
export
function
startCompass
({
interval
// TODO
}
=
{})
{
if
(
isEnable
)
{
return
}
isEnable
=
true
return
invokeMethod
(
'
enableCompass
'
,
{
enable
:
true
})
}
export
function
stopCompass
()
{
isEnable
=
false
return
invokeMethod
(
'
enableCompass
'
,
{
enable
:
false
})
}
src/platforms/app-plus/service/api/device/compass.js
浏览文件 @
e2c6b14e
...
...
@@ -3,53 +3,59 @@ import {
}
from
'
../constants
'
import
{
getLastWebview
}
from
'
../util
'
import
{
publish
invoke
}
from
'
../../bridge
'
let
watchOrientationId
=
false
let
isWatchOrientation
=
false
let
listener
const
clearWatchOrientation
=
()
=>
{
if
(
watchOrientationId
)
{
plus
.
orientation
.
clearWatch
(
watchOrientationId
)
watchOrientationId
=
false
}
}
const
callbackIds
=
[]
export
function
enableCompass
({
enable
})
{
if
(
enable
)
{
clearWatchOrientation
()
watchOrientationId
=
plus
.
orientation
.
watchOrientation
((
o
)
=>
{
publish
(
'
onCompassChange
'
,
{
direction
:
o
.
magneticHeading
,
errMsg
:
'
enableCompass:ok
'
export
function
startCompass
(
options
,
callbackId
)
{
listener
=
listener
||
plus
.
orientation
.
watchOrientation
((
res
)
=>
{
callbackIds
.
forEach
(
callbackId
=>
{
invoke
(
callbackId
,
{
direction
:
res
.
magneticHeading
})
},
(
e
)
=>
{
publish
(
'
onCompassChange
'
,
{
errMsg
:
'
enableCompass:fail
'
})
},
{
frequency
:
DEVICE_FREQUENCY
})
if
(
!
isWatchOrientation
)
{
isWatchOrientation
=
true
const
webview
=
getLastWebview
()
if
(
webview
)
{
webview
.
addEventListener
(
'
close
'
,
()
=>
{
plus
.
orientation
.
clearWatch
(
watchOrientationId
)
})
}
},
err
=>
{
listener
=
null
invoke
(
callbackId
,
{
errMsg
:
`startCompass:fail
${
err
.
message
}
`
})
},
{
frequency
:
DEVICE_FREQUENCY
})
setTimeout
(()
=>
{
invoke
(
callbackId
,
{
errMsg
:
'
startCompass:ok
'
})
},
DEVICE_FREQUENCY
)
}
export
function
stopCompass
()
{
if
(
listener
)
{
plus
.
orientation
.
clearWatch
(
listener
)
listener
=
null
}
return
{}
}
export
function
onCompassChange
(
callbackId
)
{
if
(
!
callbackIds
.
length
)
{
startCompass
()
}
callbackIds
.
push
(
callbackId
)
}
export
function
offCompassChange
(
callbackId
)
{
// 暂不支持移除所有监听
if
(
callbackId
)
{
const
index
=
callbackIds
.
indexOf
(
callbackId
)
if
(
index
>=
0
)
{
callbackIds
.
splice
(
index
,
1
)
}
}
else
{
clearWatchOrientation
()
}
return
{
errMsg
:
'
enableCompass:ok
'
if
(
!
callbackIds
.
length
)
{
stopCompass
()
}
}
src/platforms/h5/service/api/device/compass.js
浏览文件 @
e2c6b14e
const
callbacks
=
[]
var
listener
/**
* 监听罗盘数据
* @param {*} callbackId
*/
export
function
onCompassChange
(
callbackId
)
{
callbacks
.
push
(
callbackId
)
if
(
!
listener
)
{
startCompass
()
}
}
/**
* 开始监听罗盘数据
*/
export
function
startCompass
()
{
let
listener
const
callbackIds
=
[]
export
function
startCompass
(
options
,
callbackId
)
{
const
{
invokeCallbackHandler
:
invoke
}
=
UniServiceJSBridge
if
(
window
.
DeviceOrientationEvent
)
{
if
(
!
window
.
DeviceOrientationEvent
)
{
return
{
errMsg
:
'
startCompass:fail
'
}
}
function
addEventListener
()
{
listener
=
function
(
event
)
{
var
direction
=
360
-
event
.
alpha
callbacks
.
forEach
(
callbackId
=>
{
const
direction
=
360
-
event
.
alpha
callback
Id
s
.
forEach
(
callbackId
=>
{
invoke
(
callbackId
,
{
errMsg
:
'
onCompassChange:ok
'
,
direction
:
direction
||
0
})
})
}
window
.
addEventListener
(
'
deviceorientation
'
,
listener
,
false
)
return
{}
}
else
{
throw
new
Error
(
'
device nonsupport deviceorientation
'
)
}
if
(
!
listener
)
{
if
(
DeviceOrientationEvent
.
requestPermission
)
{
DeviceOrientationEvent
.
requestPermission
().
then
((
res
)
=>
{
if
(
res
===
'
granted
'
)
{
addEventListener
()
invoke
(
callbackId
,
{
errMsg
:
'
startCompass:ok
'
})
}
else
{
invoke
(
callbackId
,
{
errMsg
:
`startCompass:fail
${
res
}
`
})
}
}).
catch
(
error
=>
{
invoke
(
callbackId
,
{
errMsg
:
`startCompass:fail
${
error
}
`
})
})
return
}
addEventListener
()
}
return
{}
}
/**
* 停止监听罗盘数据
*/
export
function
stopCompass
()
{
if
(
listener
)
{
window
.
removeEventListener
(
'
deviceorientation
'
,
listener
,
false
)
...
...
@@ -43,3 +54,23 @@ export function stopCompass () {
}
return
{}
}
export
function
onCompassChange
(
callbackId
)
{
if
(
!
callbackIds
.
length
)
{
startCompass
()
}
callbackIds
.
push
(
callbackId
)
}
export
function
offCompassChange
(
callbackId
)
{
// 暂不支持移除所有监听
if
(
callbackId
)
{
const
index
=
callbackIds
.
indexOf
(
callbackId
)
if
(
index
>=
0
)
{
callbackIds
.
splice
(
index
,
1
)
}
}
if
(
!
callbackIds
.
length
)
{
stopCompass
()
}
}
src/platforms/h5/view/components/map/index.vue
浏览文件 @
e2c6b14e
<
template
>
<uni-map
:id=
"id"
v-on=
"$listeners"
v-on=
"$listeners"
>
<div
ref=
"map"
style=
"width: 100%; height: 100%; position: relative; overflow: hidden;"
style=
"width: 100%; height: 100%; position: relative; overflow: hidden;"
/>
<div
style=
"position: absolute; top: 0; width: 100%; height: 100%; overflow: hidden; pointer-events: none;"
>
<slot
/>
...
...
@@ -350,14 +350,14 @@ export default {
})
})
break
case
'
moveToLocation
'
:
{
const
{
latitude
,
longitude
}
=
data
var
locationPosition
=
(
latitude
&&
longitude
)
?
new
maps
.
LatLng
(
latitude
,
longitude
)
:
this
.
_locationPosition
if
(
locationPosition
)
{
this
.
_map
.
setCenter
(
locationPosition
)
callback
({})
}
case
'
moveToLocation
'
:
{
const
{
latitude
,
longitude
}
=
data
var
locationPosition
=
(
latitude
&&
longitude
)
?
new
maps
.
LatLng
(
latitude
,
longitude
)
:
this
.
_locationPosition
if
(
locationPosition
)
{
this
.
_map
.
setCenter
(
locationPosition
)
callback
({})
}
}
break
case
'
translateMarker
'
:
...
...
@@ -820,9 +820,10 @@ export default {
})
this
.
_location
=
location
refreshLocation
()
uni
.
onCompassChange
(
function
(
res
)
{
this
.
__onCompassChange
=
function
(
res
)
{
location
.
setRotation
(
res
.
direction
)
})
}
uni
.
onCompassChange
(
this
.
__onCompassChange
)
},
fail
:
e
=>
{
console
.
error
(
e
)
...
...
@@ -857,7 +858,7 @@ export default {
location
.
setMap
(
null
)
this
.
_location
=
null
this
.
_locationPosition
=
null
uni
.
stopCompass
(
)
uni
.
offCompassChange
(
this
.
__onCompassChange
)
}
},
fitBounds
(
points
,
cb
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录