harmony-utils之LocationUtil,定位相關工具類
harmony-utils 簡介與說明
harmony-utils 一款功能豐富且極易上手的HarmonyOS工具庫,借助眾多實用工具類,致力于助力開發者迅速構建鴻蒙應用。其封裝的工具涵蓋了APP、設備、屏幕、授權、通知、線程間通信、彈框、吐司、生物認證、用戶首選項、拍照、相冊、掃碼、文件、日志、異常捕獲、字符、字符串、數字、集合、日期、隨機、base64、加密、解密、JSON等一系列的功能和作,能夠滿足各種不同的開發需求。
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 判斷位置服務是否已經使能(定位是否開啟)
let isLocationEnabled = LocationUtil.isLocationEnabled();
ToastUtil.showToast(`定位是否開啟:${isLocationEnabled}`);
requestLocationPermissions 申請定位權限
LocationUtil.requestLocationPermissions().then((grant) = > {
if (grant) { //已授權
ToastUtil.showToast("授權成功");
} else {
ToastUtil.showToast("請在設置中開啟定位權限");
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 開啟位置變化訂閱,并發起定位請求
private locationCallBack: Callback< geoLocationManager.Location > = (location) = > {
let addrStr = `位置變化訂閱1:n${JSON.stringify(location, null, 2)}`;
LogUtil.info(addrStr);
}
LocationUtil.onLocationChangeEasy(locationCallBack);
onLocationChange 開啟位置變化訂閱,并發起定位請求
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, //表示快速獲取位置優先,如果應用希望快速拿到一個位置,可以將優先級設置為該字段。
'scenario': geoLocationManager.LocationRequestScenario.UNSET, //表示未設置優先級,表示LocationRequestPriority無效。
'timeInterval': 10, //表示上報位置信息的時間間隔,單位是秒。默認值為1,取值范圍為大于等于0。10秒鐘獲取一下位置
'distanceInterval': 0, //表示上報位置信息的距離間隔。單位是米,默認值為0,取值范圍為大于等于0。
'maxAccuracy': 0 //表示精度信息,單位是米。
}; //開啟位置變化訂閱,默認Request參數
LocationUtil.onLocationChange(locationRequest, locationCallBack);
offLocationChange 關閉位置變化訂閱,并刪除對應的定位請求
LocationUtil.offLocationChange();
onLocationError 訂閱持續定位過程中的錯誤碼
LocationUtil.onLocationError((locationError: geoLocationManager.LocationError) = > {
LogUtil.error("訂閱持續定位過程中的錯誤碼: " + locationError);
ToastUtil.showToast("訂閱持續定位過程中的錯誤碼: " + locationError);
});
offLocationError 取消訂閱持續定位過程中的錯誤碼
LocationUtil.offLocationError();
ToastUtil.showToast("取消訂閱成功");
onLocationEnabledChange 訂閱位置服務狀態變化
LocationUtil.onLocationError((locationError: geoLocationManager.LocationError) = > {
LogUtil.error("訂閱持續定位過程中的錯誤碼: " + locationError);
ToastUtil.showToast("訂閱持續定位過程中的錯誤碼: " + locationError);
});
offLocationEnabledChange 取消訂閱位置服務狀態變化
LocationUtil.offLocationEnabledChange();
ToastUtil.showToast("取消訂閱成功");
isGeocoderAvailable 判斷地理編碼與逆地理編碼服務是否可用
let isGeocoderAvailable = LocationUtil.isGeocoderAvailable();
LogUtil.error(`地理編碼與逆地理編碼服務是否可用:${isGeocoderAvailable}`);
getGeoAddressFromLocationName 地理編碼,將地理描述轉換為具體坐標集合
let locationName: string = '上海市浦東新區'; //上海市浦東新區
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 地理編碼,將地理描述轉換為具體坐標
let locationName: string = '上海市浦東新區'; //上海市浦東新區
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 逆地理編碼,將坐標轉換為地理描述集合
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 逆地理編碼,將坐標轉換為地理描述
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 坐標轉換,將WGS84坐標系轉換為GCJ02坐標系
let latLng: mapCommon.LatLng = { latitude: 31.8462, longitude: 117.2456 };
let latLng1 = LocationUtil.convertCoordinateSync(mapCommon.CoordinateType.WGS84, mapCommon.CoordinateType.GCJ02, latLng);
LogUtil.error(`坐標轉換,latLng1:${JSON.stringify(latLng1, null, 2)}`);
let latLng2 = LocationUtil.convertCoordinateEasy(latLng);
LogUtil.error(`坐標轉換,latLng2:${JSON.stringify(latLng2, null, 2)}`);
創作不易,請給童長老點贊
審核編輯 黃宇
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
Harmony
+關注
關注
0文章
104瀏覽量
2984
發布評論請先 登錄
相關推薦
熱點推薦
harmony-utils之PreviewUtil,文件預覽工具類
harmony-utils之PreviewUtil,文件預覽工具類 harmony-utils 簡介與說明 [
harmony-utils之SnapshotUtil,截圖相關工具類
harmony-utils之SnapshotUtil,截圖相關工具類 harmony-utils
harmony-utils之WindowUtil,窗口相關工具類
harmony-utils之WindowUtil,窗口相關工具類 harmony-utils 簡
harmony-utils之AuthUtil,生物認證相關工具類
# harmony-utils之AuthUtil,生物認證相關工具類 ## harmony-utils
harmony-utils之NetworkUtil,網絡相關工具類
harmony-utils之NetworkUtil,網絡相關工具類 harmony-utils
評論