女人自慰AV免费观看内涵网,日韩国产剧情在线观看网址,神马电影网特片网,最新一级电影欧美,在线观看亚洲欧美日韩,黄色视频在线播放免费观看,ABO涨奶期羡澄,第一导航fulione,美女主播操b

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

harmony-utils之LocationUtil,定位相關(guān)工具類

童長老 ? 來源:jf_14594073 ? 作者:jf_14594073 ? 2025-07-03 18:13 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

harmony-utils之LocationUtil,定位相關(guān)工具類

harmony-utils 簡介與說明


harmony-utils 一款功能豐富且極易上手的HarmonyOS工具庫,借助眾多實用工具類,致力于助力開發(fā)者迅速構(gòu)建鴻蒙應(yīng)用。其封裝的工具涵蓋了APP、設(shè)備、屏幕、授權(quán)、通知、線程間通信、彈框、吐司、生物認證、用戶首選項、拍照、相冊、掃碼、文件、日志、異常捕獲、字符、字符串、數(shù)字、集合、日期、隨機、base64、加密、解密、JSON等一系列的功能和作,能夠滿足各種不同的開發(fā)需求。
picker_utils 是harmony-utils拆分出來的一個子庫,包含 PickerUtil、PhotoHelper、ScanUtil。

下載安裝
ohpm i @pura/harmony-utils
ohpm i @pura/picker_utils

//全局初始化方法,在UIAbility的onCreate方法中初始化 AppUtil.init()
 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
   AppUtil.init(this.context);
 }

API方法與使用


isLocationEnabled 判斷位置服務(wù)是否已經(jīng)使能(定位是否開啟)
let isLocationEnabled = LocationUtil.isLocationEnabled();
 ToastUtil.showToast(`定位是否開啟:${isLocationEnabled}`);
requestLocationPermissions 申請定位權(quán)限
LocationUtil.requestLocationPermissions().then((grant) = > {
  if (grant) { //已授權(quán)
    ToastUtil.showToast("授權(quán)成功");
  } else {
    ToastUtil.showToast("請在設(shè)置中開啟定位權(quán)限");
    WantUtil.toAppSetting();
  }
});
getCurrentLocationEasy 獲取當前位置
LocationUtil.getCurrentLocationEasy().then((location) = > {
  let locationStr = `當前位置1:n${JSON.stringify(location, null, 2)}nn`;
  LocationUtil.getGeoAddressFromLocation(location.latitude, location.longitude, 2).then((address) = > {
    locationStr = locationStr + `當前位置2:n${JSON.stringify(address, null, 2)}`;
    LogUtil.error(locationStr);
  });
}).catch((err: BusinessError) = > {
  let locationStr = `當前位置~異常信息:n錯誤碼: ${err.code}n錯誤信息:${err.message}`;
  LogUtil.error(locationStr);
});
getCurrentLocation 獲取當前位置
LocationUtil.getCurrentLocation().then((location) = > {
  let locationStr = `當前位置:n${JSON.stringify(location, null, 2)}`;
  LogUtil.error(locationStr);
}).catch((err: BusinessError) = > {
  let locationStr = `當前位置~異常信息:n錯誤碼: ${err.code}n錯誤信息:${err.message}`;
  LogUtil.error(locationStr);
});
getLastLocation 獲取上一次位置
let lastLocation = LocationUtil.getLastLocation();
let locationStr = `當前位置:n${JSON.stringify(lastLocation, null, 2)}`;
LogUtil.error(locationStr);
onLocationChangeEasy 開啟位置變化訂閱,并發(fā)起定位請求
private locationCallBack: Callback< geoLocationManager.Location > = (location) = > {
  let addrStr = `位置變化訂閱1:n${JSON.stringify(location, null, 2)}`;
  LogUtil.info(addrStr);
}
LocationUtil.onLocationChangeEasy(locationCallBack);
onLocationChange 開啟位置變化訂閱,并發(fā)起定位請求
private locationCallBack: Callback< geoLocationManager.Location > = (location) = > {
  let addrStr = `位置變化訂閱1:n${JSON.stringify(location, null, 2)}`;
  LogUtil.info(addrStr);
}

let locationRequest: geoLocationManager.LocationRequest = {
  'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX, //表示快速獲取位置優(yōu)先,如果應(yīng)用希望快速拿到一個位置,可以將優(yōu)先級設(shè)置為該字段。
  'scenario': geoLocationManager.LocationRequestScenario.UNSET, //表示未設(shè)置優(yōu)先級,表示LocationRequestPriority無效。
  'timeInterval': 10, //表示上報位置信息的時間間隔,單位是秒。默認值為1,取值范圍為大于等于0。10秒鐘獲取一下位置
  'distanceInterval': 0, //表示上報位置信息的距離間隔。單位是米,默認值為0,取值范圍為大于等于0。
  'maxAccuracy': 0 //表示精度信息,單位是米。
}; //開啟位置變化訂閱,默認Request參數(shù)
LocationUtil.onLocationChange(locationRequest, locationCallBack);
offLocationChange 關(guān)閉位置變化訂閱,并刪除對應(yīng)的定位請求
LocationUtil.offLocationChange();
onLocationError 訂閱持續(xù)定位過程中的錯誤碼
LocationUtil.onLocationError((locationError: geoLocationManager.LocationError) = > {
  LogUtil.error("訂閱持續(xù)定位過程中的錯誤碼: " + locationError);
  ToastUtil.showToast("訂閱持續(xù)定位過程中的錯誤碼: " + locationError);
});
offLocationError 取消訂閱持續(xù)定位過程中的錯誤碼
LocationUtil.offLocationError();
 ToastUtil.showToast("取消訂閱成功");
onLocationEnabledChange 訂閱位置服務(wù)狀態(tài)變化
LocationUtil.onLocationError((locationError: geoLocationManager.LocationError) = > {
  LogUtil.error("訂閱持續(xù)定位過程中的錯誤碼: " + locationError);
  ToastUtil.showToast("訂閱持續(xù)定位過程中的錯誤碼: " + locationError);
});
offLocationEnabledChange 取消訂閱位置服務(wù)狀態(tài)變化
LocationUtil.offLocationEnabledChange();
 ToastUtil.showToast("取消訂閱成功");
isGeocoderAvailable 判斷地理編碼與逆地理編碼服務(wù)是否可用
let isGeocoderAvailable = LocationUtil.isGeocoderAvailable();
 LogUtil.error(`地理編碼與逆地理編碼服務(wù)是否可用:${isGeocoderAvailable}`);
getGeoAddressFromLocationName 地理編碼,將地理描述轉(zhuǎn)換為具體坐標集合
let locationName: string = '上海市浦東新區(qū)'; //上海市浦東新區(qū)
let address = await LocationUtil.getGeoAddressFromLocationName(locationName)
  .catch((err: BusinessError) = > {
    return `異常信息:${err.code}  ---  ${err.message}`;
  });
let addrStr = `地理編碼:n${JSON.stringify(address, null, 2)}`;
LogUtil.info(addrStr);
getAddressFromLocationName 地理編碼,將地理描述轉(zhuǎn)換為具體坐標
let locationName: string = '上海市浦東新區(qū)'; //上海市浦東新區(qū)
let address = await LocationUtil.getAddressFromLocationName(locationName)
  .catch((err: BusinessError) = > {
    return `異常信息:${err.code}  ---  ${err.message}`;
  });
let addrStr = `地理編碼:n${JSON.stringify(address, null, 2)}`;
LogUtil.info(addrStr);
getGeoAddressFromLocation 逆地理編碼,將坐標轉(zhuǎn)換為地理描述集合
let latitude: number = 32.26;
let longitude: number = 117.618;
let address = await LocationUtil.getGeoAddressFromLocation(latitude, longitude)
  .catch((err: BusinessError) = > {
    return `異常信息:${err.code}  ---  ${err.message}`;
  });
let addrStr = `逆地理編碼:n${JSON.stringify(address, null, 2)}`;
LogUtil.info(addrStr);
getAddressFromLocation 逆地理編碼,將坐標轉(zhuǎn)換為地理描述
let latitude: number = 32.2;
let longitude: number = 117.6;
let address = await LocationUtil.getAddressFromLocation(latitude, longitude)
  .catch((err: BusinessError) = > {
    return `異常信息:${err.code}  ---  ${err.message}`;
  });
let addrStr = `逆地理編碼:n${JSON.stringify(address, null, 2)}`;
LogUtil.info(addrStr);
getCountryCode 獲取當前的國家碼
let code = await LocationUtil.getCountryCode();
 ToastUtil.showToast(`當前的國家碼:${code}`);
calculateDistance 計算這兩個點間的直線距離,單位為米
let fromLatLng: mapCommon.LatLng = { latitude: 38, longitude: 118 };
let toLatLng: mapCommon.LatLng = { latitude: 38.5, longitude: 118.5 };
let distance1 = LocationUtil.calculateDistance(fromLatLng, toLatLng);
LogUtil.error(`距離1${distance1}米`);

let distance2 = LocationUtil.calculateDistanceEasy(38, 118, 39, 119);
LogUtil.error(`距離2${distance2}米`);
convertCoordinate 坐標轉(zhuǎn)換,將WGS84坐標系轉(zhuǎn)換為GCJ02坐標系
let latLng: mapCommon.LatLng = { latitude: 31.8462, longitude: 117.2456 };
let latLng1 = LocationUtil.convertCoordinateSync(mapCommon.CoordinateType.WGS84, mapCommon.CoordinateType.GCJ02, latLng);
LogUtil.error(`坐標轉(zhuǎn)換,latLng1${JSON.stringify(latLng1, null, 2)}`);
let latLng2 = LocationUtil.convertCoordinateEasy(latLng);
LogUtil.error(`坐標轉(zhuǎn)換,latLng2${JSON.stringify(latLng2, null, 2)}`);

創(chuàng)作不易,請給童長老點贊

審核編輯 黃宇

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • Harmony
    +關(guān)注

    關(guān)注

    0

    文章

    108

    瀏覽量

    3014
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點推薦

    harmony-utilsCacheUtil,緩存工具

    harmony-utilsCacheUtil,緩存工具
    的頭像 發(fā)表于 07-04 16:36 ?114次閱讀

    harmony-utilsCharUtil,字符工具

    harmony-utilsCharUtil,字符工具
    的頭像 發(fā)表于 07-04 16:34 ?118次閱讀

    harmony-utilsCrashUtil,異常相關(guān)工具

    harmony-utilsCrashUtil,異常相關(guān)工具
    的頭像 發(fā)表于 07-04 16:33 ?117次閱讀

    harmony-utilsDeviceUtil,設(shè)備相關(guān)工具

    harmony-utilsDeviceUtil,設(shè)備相關(guān)工具
    的頭像 發(fā)表于 07-03 18:27 ?151次閱讀

    harmony-utilsDisplayUtil,屏幕相關(guān)工具

    harmony-utilsDisplayUtil,屏幕相關(guān)工具
    的頭像 發(fā)表于 07-03 18:26 ?125次閱讀

    harmony-utilsEmitterUtil,Emitter工具

    harmony-utilsEmitterUtil,Emitter工具
    的頭像 發(fā)表于 07-03 18:24 ?130次閱讀

    harmony-utilsFileUtil,文件相關(guān)工具

    harmony-utilsFileUtil,文件相關(guān)工具
    的頭像 發(fā)表于 07-03 18:23 ?129次閱讀

    harmony-utilsFormatUtil,格式化工具

    harmony-utilsFormatUtil,格式化工具
    的頭像 發(fā)表于 07-03 18:22 ?130次閱讀

    harmony-utilsImageUtil,圖片相關(guān)工具

    harmony-utilsImageUtil,圖片相關(guān)工具
    的頭像 發(fā)表于 07-03 18:22 ?164次閱讀

    harmony-utilsPreviewUtil,文件預(yù)覽工具

    harmony-utilsPreviewUtil,文件預(yù)覽工具 harmony-utils 簡介與說明 [
    的頭像 發(fā)表于 07-03 11:40 ?110次閱讀

    harmony-utilsSnapshotUtil,截圖相關(guān)工具

    harmony-utilsSnapshotUtil,截圖相關(guān)工具 harmony-utils
    的頭像 發(fā)表于 07-03 11:36 ?106次閱讀

    harmony-utilsWindowUtil,窗口相關(guān)工具

    harmony-utilsWindowUtil,窗口相關(guān)工具 harmony-utils
    的頭像 發(fā)表于 06-30 17:33 ?106次閱讀

    harmony-utilsAuthUtil,生物認證相關(guān)工具

    # harmony-utilsAuthUtil,生物認證相關(guān)工具 ## harmony-utils
    的頭像 發(fā)表于 06-26 17:43 ?104次閱讀

    harmony-utilsNetworkUtil,網(wǎng)絡(luò)相關(guān)工具

    harmony-utilsNetworkUtil,網(wǎng)絡(luò)相關(guān)工具 harmony-utils
    的頭像 發(fā)表于 06-25 23:46 ?48次閱讀

    harmony-utilsDateUtil,日期工具

    harmony-utilsDateUtil,日期工具
    的頭像 發(fā)表于 06-25 22:15 ?55次閱讀