HarmonyOS 位置信息获取demo

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp
import { geoLocationManager } from '@kit.LocationKit';
import { abilityAccessCtrl, Context, PermissionRequestResult, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct LocationPage {
  @State message: string = '获取位置';

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('LocationPageHelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(()=>{
          let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
          let context: Context = getContext(this) as common.UIAbilityContext;
          atManager.requestPermissionsFromUser(context, ['ohos.permission.LOCATION','ohos.permission.APPROXIMATELY_LOCATION'], (err: BusinessError, data: PermissionRequestResult) => {
            if (err) {
              console.error(`requestPermissionsFromUser fail, err->${JSON.stringify(err)}`);
            } else {
              console.info('data:' + JSON.stringify(data));
              console.info('data permissions:' + data.permissions);
              console.info('data authResults:' + data.authResults);
              console.info('data dialogShownResults:' + data.dialogShownResults);
            }
          });
          let request: geoLocationManager.SingleLocationRequest = {
            'locatingPriority': geoLocationManager.LocatingPriority.PRIORITY_LOCATING_SPEED,
            'locatingTimeoutMs': 10000
          }
          try {
            geoLocationManager.getCurrentLocation(request).then((result) => { // 调用getCurrentLocation获取当前设备位置,通过promise接收上报的位置
              console.log('current location: ' + JSON.stringify(result));
              // 坐标转化为地理位置信息
              let reverseGeocodeRequest:geoLocationManager.ReverseGeoCodeRequest = {"latitude": result.latitude, "longitude": result.longitude, "maxItems": 1};
              try {
                geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest, (err, data) => {
                  if (err) {
                    console.log('getAddressesFromLocation err: ' + JSON.stringify(err));
                  } else {
                    console.log('getAddressesFromLocation data: ' + JSON.stringify(data));
                  }
                });
              } catch (err) {
                console.error("errCode:" + JSON.stringify(err));
              }
            })
              .catch((error:BusinessError) => { // 接收上报的错误码
                console.error('promise, getCurrentLocation: error=' + JSON.stringify(error));
              });
          } catch (err) {
            console.error("errCode:" + JSON.stringify(err));
          }
        })
    }
    .height('100%')
    .width('100%')
  }
}

module.json5:

'requestPermissions': [
{'name': 'ohos.permission.INTERNET'},
{
  "name": "ohos.permission.LOCATION",
"reason": '$string:app_loc',
"usedScene": {"abilities": []}
},
{
  "name": "ohos.permission.APPROXIMATELY_LOCATION",
"reason": '$string:app_name',
"usedScene": {"abilities": []}
}
]

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-geolocationmanager-V5

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 获取位置信息报错
330浏览 • 1回复 待解决
HarmonyOS 位置信息设置及跳转
193浏览 • 1回复 待解决
获取安全地理位置信息接口的实现
542浏览 • 1回复 待解决
HarmonyOS 位置信息访问权限始终允许
212浏览 • 1回复 待解决
app.getInfo获取应用配置信息为空。
836浏览 • 1回复 待解决