目录

MapComponent (Map Component)

This module provides map components. You need to provide mapOptions and a callback that can be used to obtain a MapComponentController object.

Since: 4.1.0(11)

Modules to Import

import { MapComponent } from '@kit.MapKit';

MapComponent

Provides map components and obtains MapComponentController objects through callbacks.

Decorator: @Component

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since version 4.1.0(11).

System capability: SystemCapability.Map.Core

Since: 4.1.0(11)

Parameters:

Name Type Read-Only Optional Decorator Type Description
mapOptions mapCommon.MapOptions No Yes N/A

Map initialization parameters.

Note:

If this parameter is not passed, the map cannot be created.

mapCallback AsyncCallback<map.MapComponentController> No Yes N/A

Callback function. If the map is successfully initialized, err is undefined and data is the obtained map.MapComponentController. Otherwise, err is an error object.

Note:

If this parameter is not passed, the map cannot be created.

customInfoWindow customInfoWindowCallback No Yes @BuilderParam Customizes an information window to display detailed information about a Marker.

Example:

import { map, mapCommon, MapComponent, customInfoWindowCallback } from '@kit.MapKit';
import { AsyncCallback } from '@kit.BasicServicesKit';

@Entry
@Component
struct MarkerDemo {
  private mapOptions?: mapCommon.MapOptions;
  private mapController?: map.MapComponentController;
  private mapCallback?: AsyncCallback<map.MapComponentController>;

  aboutToAppear(): void {
    this.mapOptions = {
      position: {
        target: {
          latitude: 32.120750,
          longitude: 118.788765
        },
        zoom: 15
      }
    }

    this.mapCallback = async (err, mapController) => {
      if (!err) {
        this.mapController = mapController;
        let markerOptions: mapCommon.MarkerOptions = {
          position: {
            latitude: 32.120750,
            longitude: 118.788765
          },
          clickable: true,
          // Set the information window title.
          title: "Custom Information Window"
        };
        await this.mapController?.addMarker(markerOptions);
      }
    }
  }

  build() {
    Stack() {
      Column() {
        MapComponent({
          mapOptions: this.mapOptions,
          mapCallback: this.mapCallback,
          // Customize the information window.
          customInfoWindow: this.customInfoWindow
        })
          .width('100%')
          .height('100%')
      }.width('100%')
    }.height('100%')
  }

  // Custom information window BuilderParam.
  @BuilderParam customInfoWindow: customInfoWindowCallback = this.customInfoWindowBuilder;

  // Custom information window Builder.
  @Builder
  customInfoWindowBuilder($$: map.MarkerDelegate) {
    if ($$.marker) {
      Text($$.marker.getTitle())
        .width("50%")
        .height(50)
        .backgroundColor(Color.Green)
        .textAlign(TextAlign.Center)
        .fontColor(Color.Black)
        .font({ size: 25, weight: 10, style: FontStyle.Italic })
        .border({
          width: 3,
          color: Color.Black,
          radius: 25,
          style: BorderStyle.Dashed
        })
    }
  }
}

[h2]build

build(): void

Default constructor of the MapComponent. This method cannot be directly called.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since version 4.1.0(11).

System capability: SystemCapability.Map.Core

Since: 4.1.0(11)

customInfoWindowCallback

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

Callback for a custom information window.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since version 5.0.0(12).

System capability: SystemCapability.Map.Core

Since: 5.0.0(12)

Parameters:

Name Type Mandatory Description
markerDelegate map.MarkerDelegate Yes Delegate Marker.