HarmonyOS 逆地理编码,获取经纬度,位置搜索例子

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

可以参考如下demo:

import geoLocationManager from '@ohos.geoLocationManager';
import { BusinessError } from '@ohos.base';
import abilityAccessCtrl, { PermissionRequestResult, 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

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

分享
微博
QQ
微信
回复
1天前
相关问题
如何获取经纬度示例代码
1055浏览 • 1回复 待解决
HarmonyOS 地理编码地理编码
49浏览 • 1回复 待解决
HarmonyOS 坐标系经纬度转换
323浏览 • 1回复 待解决
HarmonyOS 有关经纬度问题
76浏览 • 1回复 待解决
HarmonyOS map kit 获取地图中心经纬度
127浏览 • 1回复 待解决
HarmonyOS 获取经纬的案例
77浏览 • 1回复 待解决