文档管理中心
您当前正在浏览HarmonyOS最新文档,覆盖已发布的所有API版本,可在API参考中筛选您使用的API版本。详细的版本配套关系请参考版本说明
API参考应用服务Map Kit(地图服务)ArkTS组件MapComponent(地图组件)

MapComponent(地图组件)

本模块提供Map组件,您需要提供mapOptions和回调,通过回调获取MapComponentController对象。

起始版本: 4.1.0(11)

导入模块

PhonePC/2in1TabletWearable
收起
自动换行
深色代码主题
复制
  1. import { MapComponent } from '@kit.MapKit';

MapComponent

PhonePC/2in1TabletWearable

MapComponent提供map组件,通过回调获取MapComponentController对象。

装饰器类型: @Component

模型约束: 此接口仅可在Stage模型下使用。

元服务API: 从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Map.Core

起始版本: 4.1.0(11)

参数

展开
名称 类型 只读 可选 装饰器类型 说明
mapOptions mapCommon.MapOptions NA

地图初始化参数。

说明:

若未传递该参数,则地图无法创建。

mapCallback AsyncCallback<map.MapComponentController> NA

回调函数,当地图初始化成功,err为undefined,data为获取到的map.MapComponentController;否则为错误对象。

说明:

若未传递该参数,则地图无法创建。

customInfoWindow customInfoWindowCallback @BuilderParam 自定义信息窗,显示一个自定义样式的信息窗,可用于展示标记相关的详细信息。

示例:

收起
自动换行
深色代码主题
复制
  1. import { map, mapCommon, MapComponent, customInfoWindowCallback } from '@kit.MapKit';
  2. import { AsyncCallback } from '@kit.BasicServicesKit';
  3. @Entry
  4. @Component
  5. struct MarkerDemo {
  6. private mapOptions?: mapCommon.MapOptions;
  7. private mapController?: map.MapComponentController;
  8. private mapCallback?: AsyncCallback<map.MapComponentController>;
  9. aboutToAppear(): void {
  10. this.mapOptions = {
  11. position: {
  12. target: {
  13. latitude: 32.120750,
  14. longitude: 118.788765
  15. },
  16. zoom: 15
  17. }
  18. }
  19. this.mapCallback = async (err, mapController) => {
  20. if (!err) {
  21. this.mapController = mapController;
  22. let markerOptions: mapCommon.MarkerOptions = {
  23. position: {
  24. latitude: 32.120750,
  25. longitude: 118.788765
  26. },
  27. clickable: true,
  28. // 设置信息窗标题
  29. title: "自定义信息窗"
  30. };
  31. await this.mapController?.addMarker(markerOptions);
  32. }
  33. }
  34. }
  35. build() {
  36. Stack() {
  37. Column() {
  38. MapComponent({
  39. mapOptions: this.mapOptions,
  40. mapCallback: this.mapCallback,
  41. // 自定义信息窗
  42. customInfoWindow: this.customInfoWindow
  43. })
  44. .width('100%')
  45. .height('100%')
  46. }.width('100%')
  47. }.height('100%')
  48. }
  49. // 自定义信息窗BuilderParam
  50. @BuilderParam customInfoWindow: customInfoWindowCallback = this.customInfoWindowBuilder;
  51. // 自定义信息窗Builder
  52. @Builder
  53. customInfoWindowBuilder($$: map.MarkerDelegate) {
  54. if ($$.marker) {
  55. Text($$.marker.getTitle())
  56. .width("50%")
  57. .height(50)
  58. .backgroundColor(Color.Green)
  59. .textAlign(TextAlign.Center)
  60. .fontColor(Color.Black)
  61. .font({ size: 25, weight: 10, style: FontStyle.Italic })
  62. .border({
  63. width: 3,
  64. color: Color.Black,
  65. radius: 25,
  66. style: BorderStyle.Dashed
  67. })
  68. }
  69. }
  70. }

build

PhonePC/2in1TabletWearable

build(): void

MapComponent的默认构造函数,无法直接调用此方法。

模型约束: 此接口仅可在Stage模型下使用。

元服务API: 从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Map.Core

起始版本: 4.1.0(11)

customInfoWindowCallback

PhonePC/2in1TabletWearable

type customInfoWindowCallback = (markerDelegate: map.MarkerDelegate) => void

自定义信息窗回调。

模型约束: 此接口仅可在Stage模型下使用。

元服务API: 从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Map.Core

起始版本: 5.0.0(12)

参数

展开
参数名 类型 必填 说明
markerDelegate map.MarkerDelegate 用于表示代理对象的标记。
在 API参考 中进行搜索
请输入您想要搜索的关键词