HarmonyOS 地图组件里marker固定位置

地图组件里marker的位置如何能固定在屏幕指定位置,比如想让marker固定显示在屏幕宽度/2,屏幕高度/4的位置上,该如何设置?

HarmonyOS
2025-01-10 08:07:02
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

这边可以尝试下使用stack布局,在地图组件上叠一个marker样式。

参考如下demo:

import { map, mapCommon, MapComponent } from '@kit.MapKit';
import { AsyncCallback } from '@kit.BasicServicesKit';
// 1、导入文件
import display from '@ohos.display'

@Entry
@Component
struct Index3 {
  private mapOptions?: mapCommon.MapOptions;
  private callback?: AsyncCallback<map.MapComponentController>;
  private mapController?: map.MapComponentController;
  @State screenWidth: number = 0
  @State screenHeight: number = 0

  aboutToAppear(): void {
    // 初始化图标
    // 地图初始化参数,设置地图中心点坐标及层级
    let target: mapCommon.LatLng = {
      latitude: 39.9181,
      longitude: 116.3970193
    };
    let cameraPosition: mapCommon.CameraPosition = {
      target: target,
      zoom: 15
    };
    this.mapOptions = {
      position: cameraPosition
    };

    // 地图初始化的回调
    this.callback = async (err, mapController) => {
      if (!err) {
        this.mapController = mapController;
      }
    }

    display.getAllDisplays((err, data) => {
      let screenWidth: number = data[0].width
      let screenHeight: number = data[0].height
      console.log('width = ' + screenWidth + 'height = ' + screenHeight)
      console.log('width + height = ' + JSON.stringify(data))
    })
  }

  build() {
    Stack() {
      MapComponent({ mapOptions: this.mapOptions, mapCallback: this.callback }).width('100%').height('100%')
      marker().position({ x: "44%", y: "25%" })
    }
  }
}

@Component
struct marker {
  build() {
    Column({ space: 5 }) {
      Image($r('app.media.xxx')).width(50)
      Text("xxxx")
    }
    .width(50).height(50)
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-10 10:12:47
相关问题
HarmonyOS 地图添加marker
462浏览 • 1回复 待解决
HarmonyOS 地图自定义marker
585浏览 • 1回复 待解决
HarmonyOS 地图中如何渲染多个marker
712浏览 • 1回复 待解决
HarmonyOS 地图中是否支持海量marker
1130浏览 • 0回复 待解决
HarmonyOS 定位服务、地图服务
563浏览 • 1回复 待解决
HarmonyOS 地图定位介绍
815浏览 • 1回复 待解决
HarmonyOS地图标记和定位怎么使用
1082浏览 • 1回复 待解决
HarmonyOS 地图位置服务相关资料
624浏览 • 1回复 待解决
HarmonyOS 添加marker
420浏览 • 1回复 待解决