HarmonyOS 获取经纬的案例

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect
import geoLocationManager from '@ohos.geoLocationManager';
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';


@Entry
@Component
struct Index {
  currentLocation: geoLocationManager.Location | undefined
  private context: Context = getContext(this)
  @State currentLatitude: number = 0
  @State currentLongitude: number = 0
  private permissions: Array<Permissions> = ["ohos.permission.LOCATION", "ohos.permission.APPROXIMATELY_LOCATION"]
  private requestInfo: geoLocationManager.LocationRequest = {
    'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX,
    'scenario': geoLocationManager.LocationRequestScenario.UNSET,
    'timeInterval': 1,
    'distanceInterval': 0,
    'maxAccuracy': 0
  }
  private locationChange = (location: geoLocationManager.Location): void => {
    console.log('locationChanger: data: ' + JSON.stringify(location));
  };

  private async getLocal() {
    let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager()
    let auth = await atManager.requestPermissionsFromUser(this.context, this.permissions)
    this.currentLocation = await geoLocationManager.getCurrentLocation(this.requestInfo)
    this.currentLatitude = this.currentLocation.latitude
    this.currentLongitude = this.currentLocation.longitude
  }

  GeocoderAvailable() {
    let isAvailable = false
    try {
      isAvailable = geoLocationManager.isGeocoderAvailable();
    } catch (err) {
      console.error("errCode:" + err.code + ",errMessage:" + err.message);
    }
    return isAvailable
  }

  //通过经纬度获取详细地址
  locationNameToAddress() {
    let reverseGeocodeRequest: geoLocationManager.ReverseGeoCodeRequest =
      { "latitude": 31.12, "longitude": 121.11, "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:" + err.code + ",errMessage:" + err.message);
    }
  }

  //通过详细地址获取经纬度
  addressToLocationName() {
    let geocodeRequest: geoLocationManager.GeoCodeRequest =
      { "description": "北京市东城区东长安街16号", "maxItems": 1 };
    try {
      geoLocationManager.getAddressesFromLocationName(geocodeRequest, (err, data) => {
        if (err) {
          console.log('getAddressesFromLocationName err: ' + JSON.stringify(err));
        } else {
          console.log('getAddressesFromLocationName data: ' + JSON.stringify(data));
        }
      });
    } catch (err) {
      console.error("errCode:" + err.code + ",errMessage:" + err.message);
    }
  }

  build() {
    Row() {
      Column() {
        Text(`经度 => ${this.currentLatitude} 维度 => ${this.currentLongitude}`)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            this.getLocal()
            if (this.GeocoderAvailable()) {
              this.addressToLocationName()
              this.locationNameToAddress()
            }
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

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

需要申请以下权限:ohos.permission.LOCATION, ohos.permission.APPROXIMATELY_LOCATION, 声明权限可参考如下链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/declare-permissions-V5

分享
微博
QQ
微信
回复
3天前
相关问题
如何获取经纬度示例代码
1009浏览 • 1回复 待解决
HarmonyOS 坐标系经纬度转换
299浏览 • 1回复 待解决
HarmonyOS ArkTS接口回调案例
544浏览 • 1回复 待解决
HarmonyOS jsbridge能力暴露封装案例
234浏览 • 1回复 待解决
HarmonyOS ArkTS接口回调案例问题
676浏览 • 1回复 待解决
监听屏幕旋转案例有哪些
427浏览 • 1回复 待解决
OceanBase业务案例有哪些?
3740浏览 • 1回复 待解决
能够提供HarmonyOS自定义相机案例吗?
338浏览 • 1回复 待解决
小熊派智慧物流案例编译出错
5651浏览 • 1回复 待解决
如何实现组件水波纹动画案例
1150浏览 • 1回复 待解决