HarmonyOS Next深度解析:高德地理逆编码精准适配

发布于:2025-06-19 ⋅ 阅读:(13) ⋅ 点赞:(0)

HarmonyOS Next深度解析:高德地理逆编码精准适配

  1. HarmonyOS SDK目前不支持逆地理编码功能,本人的解决方式是:进行Web Api替代、申请高德开发平台的逆地理编码Wab API

  2. 申请高德Web Key
    在这里插入图片描述

  3. 配置AMapLocationOption:

options: AMapLocationOption = {
    locatingWithReGeocode: true, //定位是否返回逆地理信息
    isOffset: true //是否加偏
  }
  1. 示例请求:
    https://restapi.amap.com/v3/geocode/regeo?location=${经度},${纬度}&key=Web_key
  2. JSON返回数据模板为:
{"status":"1",
	"regeocode":
		{"addressComponent":
		{"city":"合肥市","province":"安徽省",
		"adcode":"340103",
		"district":"庐阳区",
		"towncode":"340103003000",
		"streetNumber":{"number":"770号","location":"117.23064,31.861242",
		"direction":"东北",
		"distance":"6.19498",
		"street":"长江中路"},
		"country":"中国",
		"township":"逍遥津街道",
		"businessAreas":[{"location":"117.279782,31.860072","name":"光明","id":"340103"},
		{"location":"117.291651,31.866126","name":"逍遥津","id":"340103"},{"location":"117.279323,31.864794","name":"城隍庙","id":"340103"}],
		"building":{"name":[],"type":[]},"neighborhood":{"name":[],"type":[]},"citycode":"0551"},
		"formatted_address":"安徽省合肥市庐阳区逍遥津街道长江中路770号安徽省政协书画研究院"},
	"info":"OK","infocode":"10000"}
  1. 获取具体地理信息的方法:
  • 其中HttpRequest.get方法是作者自行封装的网络请求,Model为GDReGeoData。
import { HttpRequest } from '../util/HttpRequest';
import { GDReGeoData } from '../model/GDReGeoData';
---
  @State formatted_address: string = '';
---
// 带安全配置的GET请求
  private async getAddressRequest(location: AMapLocation) {
    try {
      const longitude = location.longitude;
      const latitude = location.latitude;

      const url = `https://restapi.amap.com/v3/geocode/regeo?location=${longitude},${latitude}&key=Web_Key`;

      const resultData = await HttpRequest.get<GDReGeoData>(
        url,
        { remoteValidation: 'skip' }
      );

      if (resultData.regeocode) {
        const address = resultData.regeocode.addressComponent;
        console.info(`国家:${address.country}`);
        console.info(`省份:${address.province}`);
        console.info(`城市:${address.city}`);
        console.info(`区县:${address.district}`);
        console.info(`街道:${address.township}`);
        console.info(`街道号码:${address.streetNumber?.street || ''}${address.streetNumber?.number || ''}`);
        console.info(`完整地址:${resultData.regeocode.formatted_address}`);
        this.formatted_address= resultData.regeocode.formatted_address;
      }
    } catch (error) {
      console.error(`逆地理请求错误: ${error.message}`);
    }
  }
  • GDReGeoData封装的Model
export interface GDReGeoData {
  status: string;
  regeocode: ReGeocode;
  info: string;
  infocode: string;
}

interface ReGeocode {
  addressComponent: AddressComponent;
  formatted_address: string;
}

interface AddressComponent {
  city: string;
  province: string;
  adcode: string;
  district: string;
  towncode: string;
  streetNumber: StreetNumber;
  country: string;
  township: string;
  businessAreas: BusinessArea[];
  building: Dictionary;
  neighborhood: Dictionary;
}

interface StreetNumber {
  number: string;
  location: string;
  direction: string;
  distance: string;
  street: string;
}

interface BusinessArea {
  location: string;
  name: string;
  id: string;
}

interface Dictionary {
  name: string[];
  type: string[];
}

  1. 在定位监听器listener: IAMapLocationListener中调用getAddressRequest。
listener: IAMapLocationListener = {
    onLocationChanged: (location) => {
      // 调用获取具体地理信息的方法
      this.getAddressRequest(location);
    }, onLocationError: (error) => {
      this.response = JSON.stringify(error);
    }
  };
  1. 通过Text组件进行展示:
Text("具体地址信息:" + this.formatted_address).fontSize(20)
  1. 具体效果:
    在这里插入图片描述
    在这里插入图片描述
  • 补充:其中的网络请求工具,可以参考作者后续的文章。

网站公告

今日签到

点亮在社区的每一天
去签到