let markerOptions: mapCommon.MarkerOptions = {
position: {
latitude: 32.120750,
longitude: 118.788765
},
clickable: true,
// 设置信息窗标题,点击标记后可展示信息窗
title: '☆点击收藏'
};
await this.mapController?.addMarker(markerOptions); // 自定义信息窗BuilderParam
@BuilderParam customInfoWindow: ($$: map.MarkerDelegate) => void = this.customInfoWindowBuilder;
// 自定义信息窗Builder
@Builder
customInfoWindowBuilder($$: map.MarkerDelegate) {
if ($$.marker) {
Text($$.marker.getTitle())
.width(120)
.height(30)
.margin({ bottom: 3 })
.backgroundColor(Color.White)
.textAlign(TextAlign.Center)
.fontColor(Color.Black)
.font({ size: 18, weight: 10 })
.border({ width: 1, color: Color.Black, radius: 8 })
.onClick(() => {
$$.marker?.setTitle('★已收藏');
});
}
}
build() {
Stack() {
Column() {
MapComponent({
mapOptions: this.mapOptions,
mapCallback: this.callback,
// 自定义信息窗
customInfoWindow: this.customInfoWindow
})
.height(this.mapHeight);
}.width('100%');
}.ignoreLayoutSafeArea();
}
实现效果:

import { MapComponent, mapCommon, map } from '@kit.MapKit';
import { AsyncCallback } from '@kit.BasicServicesKit';
import { display } from '@kit.ArkUI';
@Entry
@Component
struct MarkerDemo {
private mapOptions?: mapCommon.MapOptions;
private mapController?: map.MapComponentController;
private callback?: AsyncCallback<map.MapComponentController>;
@State mapHeight: number = 0;
aboutToAppear(): void {
let displayClass = display.getDefaultDisplaySync();
this.mapHeight = this.getUIContext().px2vp(displayClass.height);
this.mapOptions = {
position: {
target: {
latitude: 32.120750,
longitude: 118.788765
},
zoom: 15
}
};
this.callback = async (err, mapController) => {
if (!err) {
this.mapController = mapController;
let markerOptions: mapCommon.MarkerOptions = {
position: {
latitude: 32.120750,
longitude: 118.788765
},
clickable: true,
// 设置信息窗标题,点击标记后可展示信息窗
title: '☆点击收藏'
};
await this.mapController?.addMarker(markerOptions);
} else {
console.error(`Failed to initialize the map, code is:${err.code}, message is ${err.message}`);
}
};
}
build() {
Stack() {
Column() {
MapComponent({
mapOptions: this.mapOptions,
mapCallback: this.callback,
// 自定义信息窗
customInfoWindow: this.customInfoWindow
})
.height(this.mapHeight);
}.width('100%');
}.ignoreLayoutSafeArea();
}
// 自定义信息窗BuilderParam
@BuilderParam customInfoWindow: ($$: map.MarkerDelegate) => void = this.customInfoWindowBuilder;
// 自定义信息窗Builder
@Builder
customInfoWindowBuilder($$: map.MarkerDelegate) {
if ($$.marker) {
Text($$.marker.getTitle())
.width(120)
.height(30)
.margin({ bottom: 3 })
.backgroundColor(Color.White)
.textAlign(TextAlign.Center)
.fontColor(Color.Black)
.font({ size: 18, weight: 10 })
.border({ width: 1, color: Color.Black, radius: 8 })
.onClick(() => {
$$.marker?.setTitle('★已收藏');
});
}
}
}