HarmonyOS 地图组件自定义信息窗 无法实现效果

在使用customInfoWindow这个新功能给marker添加信息窗的时候 这样的写法不弹出如何处理

HarmonyOS
2024-12-23 17:09:17
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

参考DEMO:

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

@Entry
@Component
export struct Test {
  private mapOption?: mapCommon.MapOptions;
  private mapController?: map.MapComponentController;
  private callback?: AsyncCallback<map.MapComponentController>;
  private markers: map.Marker[] = []
  private nums: string = ''

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

    // 地图初始化的回调
    this.callback = async (err, mapController) => {
      if (!err) {
        // 获取地图的控制器类,用来操作地图
        this.mapController?.setMyLocationEnabled(true)
        this.mapController?.setMyLocationControlsEnabled(true)
        this.mapController = mapController;

        this.mapController.on("mapClick", (position) => {
          console.info("mapClick", `on-mapClick position = ${position.longitude}`);
        });
        this.mapController.on("markerClick", (marker) => {
          console.info(`on-markerClick position = ${JSON.stringify(marker)}`)
          marker.setInfoWindowVisible(true);
        });

      }
    }
  }

  async setMarkerWithHotels() {
    // this.mapController?.clear()

    for (let i = 0; i < 10; i++) {
      let markerOptions: mapCommon.MarkerOptions = {
        position: {
          latitude: 32.120750 + i * 0.01,
          longitude: 118.788765 + i * 0.01
        },
        rotation: 0,
        title: String(i),
        visible: true,
        zIndex: 0,
        alpha: 1,
        anchorU: 0.5,
        anchorV: 1,
        clickable: true,
        draggable: false,
        flat: false,

        icon: $r("app.media.startIcon")
      };
      console.log(`markerOptions+${JSON.stringify(markerOptions)}`)
      // 新建marker
      let marker = await this.mapController?.addMarker(markerOptions);
      console.log(`marker+${JSON.stringify(marker)}`)
      if (marker) {
        this.markers.push(marker)
        console.log(`marker+${JSON.stringify(this.markers.push(marker))}`)
      }
    }
  }

  build() {
    NavDestination() {
      Stack() {
        Column() {
          MapComponent({
            mapOptions: this.mapOption,
            mapCallback: this.callback,
            // 自定义信息窗
            customInfoWindow: this.customInfoWindow
          })
            .width('100%')
            .height('100%');
        }.width('100%')

        Button() {
          Text('setMarkerWithHotels')
        }.onClick(() => {
          this.setMarkerWithHotels()
        })
      }.height('100%')
    }
  }

  // 自定义信息窗BuilderParam
  @BuilderParam customInfoWindow: ($$: map.MarkerDelegate) => void = this.customInfoWindowBuilder;

  // 自定义信息窗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
        })
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
分享
微博
QQ
微信
回复
2024-12-23 20:23:12
相关问题
HarmonyOS 地图信息可以自定义布局吗
1092浏览 • 1回复 待解决
HarmonyOS 地图自定义弹窗消息
814浏览 • 1回复 待解决
HarmonyOS 地图自定义信息框不生效
710浏览 • 1回复 待解决
HarmonyOS CoverFlow效果自定义组件实现
1166浏览 • 1回复 待解决
HarmonyOS 地图组件如何添加自定义UI
631浏览 • 1回复 待解决
HarmonyOS 地图自定义marker
790浏览 • 1回复 待解决
HarmonyOS 地图自定义气泡功能
691浏览 • 1回复 待解决
HarmonyOS 自定义StepperView组件如何实现
887浏览 • 1回复 待解决
HarmonyOS 定义自定义组件
1075浏览 • 1回复 待解决
HarmonyOS自定义组件增加方法如何实现
1315浏览 • 1回复 待解决
组件自定义回调函数实现
1417浏览 • 1回复 待解决