地图定位功能相信很多人都用过,在鸿蒙的应用程序开发中,使用高德地图的定位功能变得十分常见,那么在鸿蒙中一次完整的地位功能怎么实现?如果定位失败了,能否获取前几次的定位呢?本篇文章带你实现一个完整的定位功能流程,建议点赞收藏!
要想实现一个完整的定位需求流程,就必须要做好准备工作,了解实现需求的具体步骤。
要想实现一次完整的定位流程,必须根据需要分析一步步去实现,由于高德地图的引入太过于简单,这里一笔带过。重点讲解完整实现的步骤。
{
// user_grant
"name": "ohos.permission.APPROXIMATELY_LOCATION",
"reason": "$string:location_permissions_reason",
"usedScene": {
"abilities": [
"EntryAbility"
],
"when": "always"
}
},
{
// user_grant
"name": "ohos.permission.LOCATION",
"reason": "$string:location_permissions_reason",
"usedScene": {
"abilities": [
"EntryAbility"
],
"when": "always"
}
},
let location = geoLocationManager.isLocationEnabled()
if (!location) {
let dialog = new OpenSystemGPSEHelper()
dialog.show(this.getUIContext(),getContext(this) as common.UIAbilityContext,()=>{
this.currentCity = "定位失败"
})
}
//GPS跳转页面
context.startAbility(
{
bundleName: "com.huawei.hmos.settings",
abilityName: "com.huawei.hmos.settings.MainAbility",
uri: "location_manager_settings"
},
static applyPermission(context: common.UIAbilityContext, permissions: Array<Permissions>, grantedBlock: () => void,
deniedBlock?: () => void) {
let atManager = abilityAccessCtrl.createAtManager()
let permissionGrantedNumber: number = 0
atManager.requestPermissionsFromUser(context, permissions).then((data) => {
for (let index = 0; index < data.authResults.length; index++) {
if (data.authResults[index] == 0) {
permissionGrantedNumber++;
}
}
if (permissionGrantedNumber == permissions.length) {
grantedBlock()
} else {
if (deniedBlock) {
deniedBlock()
} else {
PermissionUtil.openPermissionsInSystemSettings(context)
}
}
})
}
let wantInfo: Want = {
bundleName: 'com.huawei.hmos.settings',
abilityName: 'com.huawei.hmos.settings.MainAbility',
uri: 'application_info_entry',
parameters: {
settingsParamBundleName: bundleInfo.name
}
}
context.startAbility(wantInfo).then(() => {
})
let listener: IAMapLocationListener = {
onLocationChanged: (location) => {
console.log("当前定位1:"+location.latitude+",---longitude:"+location.longitude)
this.transformCity(location.latitude,location.longitude)
}, onLocationError: (error) => {
}
};
LocationManager.getInstance().addListener(listener)
LocationManager.getInstance().initLocation()
// 定位参数配置
let options: AMapLocationOption = {
//定位优先配置选项
priority: geoLocationManager.LocationRequestPriority.FIRST_FIX,
//定位场景设置
scenario: geoLocationManager.LocationRequestScenario.UNSET,
//定位精度 单位:米
maxAccuracy: 0,
//指定单次定位超时时间
singleLocationTimeout: 3000,
//定位是否返回逆地理信息
locatingWithReGeocode: true,
//逆地址语言类型
reGeocodeLanguage: AMapLocationReGeocodeLanguage.Chinese,
isOffset: false //是否加偏
}
// 设置配置
this.locationManger?.setLocationOption(AMapLocationType.Single, options)
if (this.listener != undefined) {
// 监听
this.locationManger?.setLocationListener(AMapLocationType.Single, this.listener)
}
// 启动定位
this.locationManger?.requestSingleLocation()
let reverseGeocodeRequest:geoLocationManager.ReverseGeoCodeRequest = {"latitude":latitude, "longitude":longitude, "maxItems": 1};
try {
geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest, (err, data) => {})
LocationManager.getInstance().getLastLocation({
onLocationChanged: (location) => {
console.info('地图定位缓存获取成功: ' + JSON.stringify(location))
if (success) {
success()
}
}, onLocationError: (e) => {
console.info('地图定位缓存获取失败: ' + JSON.stringify(e))
if (success) {
success()
}
}
})
定位功能实现起来比较简单,但是完整的定位流程及细节处理才是本篇文章的关键,相信看完本篇文章你已经学会在鸿蒙中怎么使用高德定位功能了,快去动手尝试一下吧!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。