找回密码
 立即注册
首页 业界区 业界 如何在应用中实现地图关键字搜索和标记聚合功能? ...

如何在应用中实现地图关键字搜索和标记聚合功能?

枢覆引 2025-6-17 16:20:54
在如今的移动应用中,地图展示与标记功能已成为众多生活服务类应用的核心需求。无论是旅行类应用中的景点搜索与导航,还是共享类应用中的资源定位与管理,地图服务都扮演着至关重要的角色。以旅行类应用为例,用户可以通过地图快速搜索并浏览附近的景点信息,而共享单车类应用则能实时显示周边可用单车的分布情况,极大提升了用户体验。
HarmonyOS SDK为开发者提供了强大的地图服务能力,支持从地图绘制到标记点展示的全流程功能。通过其位置搜索与聚合标记技术,开发者可以轻松实现基于不同比例尺的标记点聚合,从而优化地图展示效果。本文将详细介绍如何利用地图服务的关键字搜索能力,实现附近服务的搜索与地图标记展示功能,为旅行、共享等场景提供高效的地图解决方案。
开发步骤

地图显示


  • 导入Map Kit相关模块。
  1. import { MapComponent, mapCommon, map } from '@kit.MapKit';
  2. import { AsyncCallback } from '@kit.BasicServicesKit';
复制代码

  • 新建地图初始化参数mapOptions,设置地图中心点坐标及层级。
通过callback回调的方式获取MapComponentController对象,用来操作地图。
调用MapComponent组件,传入mapOptions和callback参数,初始化地图。
  1. @Entry
  2. @Component
  3. struct HuaweiMapDemo {
  4.   private TAG = "HuaweiMapDemo";
  5.   private mapOptions?: mapCommon.MapOptions;
  6.   private callback?: AsyncCallback<map.mapcomponentcontroller>;
  7.   private mapController?: map.MapComponentController;
  8.   private mapEventManager?: map.MapEventManager;
  9.   aboutToAppear(): void {
  10.     // 地图初始化参数,设置地图中心点坐标及层级
  11.     this.mapOptions = {
  12.       position: {
  13.         target: {
  14.           latitude: 39.9,
  15.           longitude: 116.4
  16.         },
  17.         zoom: 10
  18.       }
  19.     };
  20.     // 地图初始化的回调
  21.     this.callback = async (err, mapController) => {
  22.       if (!err) {
  23.         // 获取地图的控制器类,用来操作地图
  24.         this.mapController = mapController;
  25.         this.mapEventManager = this.mapController.getEventManager();
  26.         let callback = () => {
  27.           console.info(this.TAG, `on-mapLoad`);
  28.         }
  29.         this.mapEventManager.on("mapLoad", callback);
  30.       }
  31.     };
  32.   }
  33.   // 页面每次显示时触发一次,包括路由过程、应用进入前台等场景,仅@Entry装饰的自定义组件生效
  34.   onPageShow(): void {
  35.     // 将地图切换到前台
  36.     if (this.mapController) {
  37.       this.mapController.show();
  38.     }
  39.   }
  40.   // 页面每次隐藏时触发一次,包括路由过程、应用进入后台等场景,仅@Entry装饰的自定义组件生效
  41.   onPageHide(): void {
  42.     // 将地图切换到后台
  43.     if (this.mapController) {
  44.       this.mapController.hide();
  45.     }
  46.   }
  47.   build() {
  48.     Stack() {
  49.       // 调用MapComponent组件初始化地图
  50.       MapComponent({ mapOptions: this.mapOptions, mapCallback: this.callback }).width('100%').height('100%');
  51.     }.height('100%')
  52.   }
  53. }
复制代码
关键字搜索


  • 导入相关模块。
  1. import { site } from '@kit.MapKit';
复制代码

  • 通过指定的关键字和可选的地理范围,查询诸如旅游景点、企业和学校之类的地点。
  1. let params: site.SearchByTextParams = {
  2.   // 指定关键字
  3.   query: "Piazzale Dante, 41, 55049 Viareggio, Tuscany, Italy",
  4.   // 经纬度坐标
  5.   location: {
  6.     latitude: 31.984,
  7.     longitude: 118.76625
  8.   },
  9.   // 指定地理位置的范围半径
  10.   radius: 10000,
  11.   language: "en"
  12. };
  13. // 返回关键字搜索结果
  14. const result = await site.searchByText(params);
  15. console.info("Succeeded in searching by text.");
复制代码
点聚合


  • 导入相关模块。
  1. import { map, mapCommon, MapComponent } from '@kit.MapKit';
  2. import { AsyncCallback } from '@kit.BasicServicesKit';
复制代码

  • 新增聚合图层。
  1. @Entry
  2. @Component
  3. struct ClusterOverlayDemo {
  4.   private mapOptions?: mapCommon.MapOptions;
  5.   private mapController?: map.MapComponentController;
  6.   private callback?: AsyncCallback<map.mapcomponentcontroller>;
  7.   aboutToAppear(): void {
  8.     this.mapOptions = {
  9.       position: {
  10.         target: {
  11.           latitude: 31.98,
  12.           longitude: 118.7
  13.         },
  14.         zoom: 7
  15.       }
  16.     }
  17.     this.callback = async (err, mapController) => {
  18.       if (!err) {
  19.         this.mapController = mapController;
  20.         // 生成待聚合点
  21.         let clusterItem1: mapCommon.ClusterItem = {
  22.           position: {
  23.             latitude: 31.98,
  24.             longitude: 118.7
  25.           }
  26.         };
  27.         let clusterItem2: mapCommon.ClusterItem = {
  28.           position: {
  29.             latitude: 32.99,
  30.             longitude: 118.9
  31.           }
  32.         };
  33.         let clusterItem3: mapCommon.ClusterItem = {
  34.           position: {
  35.             latitude: 31.5,
  36.             longitude: 118.7
  37.           }
  38.         };
  39.         let clusterItem4: mapCommon.ClusterItem = {
  40.           position: {
  41.             latitude: 30,
  42.             longitude: 118.7
  43.           }
  44.         };
  45.         let clusterItem5: mapCommon.ClusterItem = {
  46.           position: {
  47.             latitude: 29.98,
  48.             longitude: 117.7
  49.           }
  50.         };
  51.         let clusterItem6: mapCommon.ClusterItem = {
  52.           position: {
  53.             latitude: 31.98,
  54.             longitude: 120.7
  55.           }
  56.         };
  57.         let clusterItem7: mapCommon.ClusterItem = {
  58.           position: {
  59.             latitude: 25.98,
  60.             longitude: 119.7
  61.           }
  62.         };
  63.         let clusterItem8: mapCommon.ClusterItem = {
  64.           position: {
  65.             latitude: 30.98,
  66.             longitude: 110.7
  67.           }
  68.         };
  69.         let clusterItem9: mapCommon.ClusterItem = {
  70.           position: {
  71.             latitude: 30.98,
  72.             longitude: 115.7
  73.           }
  74.         };
  75.         let clusterItem10: mapCommon.ClusterItem = {
  76.           position: {
  77.             latitude: 28.98,
  78.             longitude: 122.7
  79.           }
  80.         };
  81.         let array: Array<mapcommon.clusteritem> = [
  82.           clusterItem1,
  83.           clusterItem2,
  84.           clusterItem3,
  85.           clusterItem4,
  86.           clusterItem5,
  87.           clusterItem6,
  88.           clusterItem7,
  89.           clusterItem8,
  90.           clusterItem9,
  91.           clusterItem10
  92.         ]
  93.         for(let index = 0; index < 100; index++){
  94.           array.push(clusterItem1)
  95.         }
  96.         for(let index = 0; index < 10; index++){
  97.           array.push(clusterItem2)
  98.         }
  99.         // 生成聚合图层的入参 聚合distance设置为100vp
  100.         let clusterOverlayParams: mapCommon.ClusterOverlayParams = { distance: 100, clusterItems: array };
  101.         // 调用addClusterOverlay生成聚合图层
  102.         let clusterOverlay: map.ClusterOverlay = await this.mapController.addClusterOverlay(clusterOverlayParams);
  103.       }
  104.     }
  105.   }
  106.   build() {
  107.     Stack() {
  108.       Column() {
  109.         MapComponent({ mapOptions: this.mapOptions, mapCallback: this.callback })
  110.           .width('100%')
  111.           .height('100%');
  112.       }.width('100%')
  113.     }.height('100%')
  114.   }
  115. }
复制代码
这里仅展示用到的主要代码及功能,具体代码可参见地图服务官网。
了解更多详情>>
访问地图服务联盟官网
获取显示地图开发指导文档
获取Poi搜索开发指导文档
获取点聚合开发指导文档

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册