一、前言
移動應用中經(jīng)常需要獲取設備的位置信息,因此在鴻蒙應用開發(fā)學習中,如何獲取手機的位置信息是必修課。之前我想偷懶從別人那里復制黏貼代碼,于是在百度上搜了一下,可能是我輸入的關(guān)鍵字不對,結(jié)果沒有找到想要的資料。于是我只能到官網(wǎng)上學習相關(guān)的開發(fā)文檔(位置服務開發(fā)指南),自己摸索著做了,經(jīng)過一番的學習,并在真機上測試,實現(xiàn)了獲取手機位置信息的功能。特記之,已備忘。
二、實現(xiàn)方法
1. 首先在module.json5(位于entry/src/main文件夾下)文件中添加應用權(quán)限。
"requestPermissions": [
{
"name": "ohos.permission.APPROXIMATELY_LOCATION"
},
{
"name": "ohos.permission.LOCATION"
}
]
2、在pages文件夾下通過“新建-page”創(chuàng)建一個ets文件,在文件中設置一個獲取位置的按鈕和用于顯示位置信息的文本組件(具體代碼見最后)
3、在這個ets文件導入?@ohos.geoLocationManager
import geoLocationManager from '@ohos.geoLocationManager'
4、實例化LocationRequest對象,用于告知系統(tǒng)該向應用提供何種類型的位置服務,以及位置結(jié)果上報的頻率。我看了開發(fā)文檔后,選擇了方式二中的代碼。
let requestInfo = {
'priority': geoLocationManager.LocationRequestPriority.ACCURACY,
'timeInterval': 0,
'distanceInterval': 0,
'maxAccuracy': 0
};
5、創(chuàng)建一個函數(shù),這個函數(shù)通過點擊界面中獲取位置按鈕來執(zhí)行。函數(shù)實現(xiàn)以下功能:
(1)實例化Callback對象,用于向系統(tǒng)提供位置上報的途徑。
(2)啟動定位。
(3)獲取系統(tǒng)緩存的最近一次歷史定位結(jié)果。
(4)結(jié)束定位。
注:函數(shù)中的幾個this開頭的變量在主程序中以@state方法修飾,用于在文本控件中顯示獲取到的位置信息。
getLocation() {
let locationChange = (location) => {
console.log('locationChanger: data: ' + JSON.stringify(location));
};
geoLocationManager.on('locationChange', requestInfo, locationChange);
try {
let location = geoLocationManager.getLastLocation();
this.mLatitude = location.latitude.toString();
this.mLongitude = location.longitude.toString();
this.mAltitude = location.altitude.toString();
this.mAccuracy = location.accuracy.toString();
this.mSpeed = location.speed.toString();
this.mTimeStamp = location.timeStamp.toString();
this.mDirection = location.direction.toString();
console.log("testTag", "獲取到的位置信息:")
console.log("testTag", "緯度latitude " + this.mLatitude)
console.log("testTag", "經(jīng)度longitude " + this.mLongitude)
console.log("testTag", "海拔(米)altitude " + this.mAltitude)
console.log("testTag", "精度(米)accuracy " + this.mAccuracy)
console.log("testTag", "速度(米/秒)speed " + this.mSpeed)
console.log("testTag", "時間戳timeStamp " + this.mTimeStamp)
console.log("testTag", "方向direction " + this.mDirection)
} catch (err) {
console.error("errCode:" + err.code + ",errMessage:" + err.message);
}
geoLocationManager.off('locationChange', locationChange);
}
6、真機調(diào)試,安裝了APP后,需要進入手機的“設置-應用和服務-應用管理”,找到安裝的APP,手動將位置信息權(quán)限打開。(因為我還沒有找到,自動開啟權(quán)限的方式,只能手動開啟)。
7、運行APP,進入手機定位頁面,點擊“獲取位置”按鈕,界面顯示出了獲取到的相關(guān)信息。
文章來源:http://www.zghlxwxcb.cn/news/detail-820752.html
三、源代碼
最后上我寫的ets文件源代碼。文章來源地址http://www.zghlxwxcb.cn/news/detail-820752.html
import geoLocationManager from '@ohos.geoLocationManager'
let requestInfo = {
'priority': geoLocationManager.LocationRequestPriority.ACCURACY,
'timeInterval': 0,
'distanceInterval': 0,
'maxAccuracy': 0
};
@Entry
@Component
struct LocationPage {
@State mLatitude: string = '' // 經(jīng)度
@State mLongitude: string = '' // 緯度
@State mAltitude: string = '' // 海拔(米)
@State mAccuracy: string = '' // 精度(米)
@State mSpeed: string = '' //速度(米/秒)
@State mTimeStamp: string = '' // 時間戳
@State mDirection: string = '' // 方向
build() {
Column() {
Button("獲取位置")
.width(100)
.backgroundColor($r('app.color.button_bgColor_lightBlue'))
.margin({ top: 50, bottom: 20 })
.onClick(() => {
this.getLocation()
})
Text('當前位置')
.fontSize(24)
Grid() {
GridItem() {
Text('經(jīng)度:')
}
GridItem() {
Text(this.mLatitude)
}
GridItem() {
Text('緯度:')
}
GridItem() {
Text(this.mLongitude)
}
GridItem() {
Text('海拔:')
}
GridItem() {
Text(this.mAltitude)
}
GridItem() {
Text('精度:')
}
GridItem() {
Text(this.mAccuracy)
}
GridItem() {
Text('速度:')
}
GridItem() {
Text(this.mSpeed)
}
GridItem() {
Text('時間:')
}
GridItem() {
Text(this.mSpeed)
}
GridItem() {
Text('方向:')
}
GridItem() {
Text(this.mDirection)
}
}
.columnsTemplate('1fr 4fr')
.rowsGap(15)
.padding(10)
.width('90%')
}
.width('100%')
.backgroundColor('#EAEAEA')
.padding(10)
}
// 獲取手機當前位置
getLocation() {
let locationChange = (location) => {
console.log('locationChanger: data: ' + JSON.stringify(location));
};
geoLocationManager.on('locationChange', requestInfo, locationChange);
try {
let location = geoLocationManager.getLastLocation();
this.mLatitude = location.latitude.toString();
this.mLongitude = location.longitude.toString();
this.mAltitude = location.altitude.toString();
this.mAccuracy = location.accuracy.toString();
this.mSpeed = location.speed.toString();
this.mTimeStamp = location.timeStamp.toString();
this.mDirection = location.direction.toString();
console.log("testTag", "獲取到的位置信息:")
console.log("testTag", "緯度latitude " + this.mLatitude)
console.log("testTag", "經(jīng)度longitude " + this.mLongitude)
console.log("testTag", "海拔(米)altitude " + this.mAltitude)
console.log("testTag", "精度(米)accuracy " + this.mAccuracy)
console.log("testTag", "速度(米/秒)speed " + this.mSpeed)
console.log("testTag", "時間戳timeStamp " + this.mTimeStamp)
console.log("testTag", "方向direction " + this.mDirection)
} catch (err) {
console.error("errCode:" + err.code + ",errMessage:" + err.message);
}
geoLocationManager.off('locationChange', locationChange);
}
}
到了這里,關(guān)于鴻蒙應用開發(fā)學習:獲取手機位置信息的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!