初
希望能寫一些簡單的教程和案例分享給需要的人
鴻蒙可穿戴開發(fā)
環(huán)境與設(shè)備
系統(tǒng):window
設(shè)備:HUAWEI WATCH 3 Pro
開發(fā)工具:DevEco Studio 3.0.0.800
鴻蒙開發(fā)
文件夾:
entry:項目文件夾
js:前端文件夾
pages:頁面文件夾
index:首頁文件夾
文件
index.css:首頁樣式
index.hml:首頁
index.js:首頁腳本
新增第二頁面
操作步驟:
首先在pages鼠標(biāo)右擊;
點擊New----第二欄菜單點擊Js Page;
在文本框中輸入頁面名稱(geolocation)名稱可以自己擬定
點擊彈框右下角Finsin按鈕完成頁面創(chuàng)建
需要引用包
import geolocation from ‘@system.geolocation’;
代碼如下:
geolocation.hml
<div class="container">
<text class="title">
位置 {{ latitude }},{{ longitude }}
</text>
</div>
geolocation.js
import geolocation from '@system.geolocation';
export default {
data: {
latitude: '...',
longitude: '...'
},
onInit() {
console.info("dao_logger::定位獲取");
geolocation.subscribe({
success: (data) => {
console.info('dao_logger::位置訂閱更新' + data.latitude + ',' + data.longitude)
this.latitude = data.latitude;
this.longitude = data.longitude;
},
fail: function (data, code) {
console.info('dao_logger:: fail to get location. code:' + code + ', data:' + data);
},
});
}
}
修改首頁 -> 新建按鈕 “ 跳轉(zhuǎn) ”
index.hml
在HTML文件“index.hml”,添加按鈕,這里按鈕用到是<input>標(biāo)簽
標(biāo)簽屬性:
type=“button”【規(guī)定 input 元素的類型】
<div class="container">
<text class="title">
你好,我是首頁
</text>
<input else class="btn" type="button" value="跳轉(zhuǎn)" onclick="onClickTest"></input>
</div>
index.js 引用包:‘@system.router’
onInit() : 進(jìn)入頁面初始化運(yùn)行的方法
onClickTest () :按鈕點擊后觸發(fā)的方法,我們將跳轉(zhuǎn)頁面的代碼寫在這個位置就可以實現(xiàn)點擊按鈕進(jìn)行跳轉(zhuǎn)頁面的動作
import router from '@system.router';
export default {
data: {
title: ""
},
onInit() {
this.title = this.$t('strings.world');
},
onClickTest() {
router.push({
uri: "pages/geolocation/geolocation",
});
}
}
首頁效果
點擊結(jié)果按鈕跳轉(zhuǎn)后 :
點擊按鈕后,最新日志顯示獲取到的定位
手表上的展示結(jié)果
手表顯示獲取到的位置信息
遇到報錯
遇到報錯 fail to get location. code:601, data:user rejects the perssion request ,是因為沒有加入權(quán)限,如下圖報錯日志中顯示的
我們在 config.json 里面加入權(quán)限,如下圖所示:文章來源:http://www.zghlxwxcb.cn/news/detail-608231.html
"reqPermissions": [
{
"name": "ohos.permission.LOCATION"
},
{
"name": "ohos.permission.MEDIA_LOCATION"
}
]
文章來源地址http://www.zghlxwxcb.cn/news/detail-608231.html
到了這里,關(guān)于華為手表開發(fā):WATCH 3 Pro(8)獲取位置服務(wù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!