HarmonyOS Map Kit '我的位置'按钮如何更改位置

地图界面中,"我的位置"按钮能否移动到其他位置,一直默认位于右下角。没找到响应的api

HarmonyOS
2024-08-14 16:14:33
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

目前该按钮是无法更改位置的

可以尝试自己写个组件传mapController进去

demo如下:

import { MapComponent, mapCommon, map, sceneMap } from '@kit.MapKit'; 
import { AsyncCallback } from '@kit.BasicServicesKit'; 
import BusinessError from "@ohos.base"; 
import { geoLocationManager } from '@kit.LocationKit'; 
 
 
@Entry 
@Component 
struct HuaweiMapDemo { 
  private mapOption?: mapCommon.MapOptions; 
  private callback?: AsyncCallback<map.MapComponentController>; 
  private mapController: map.MapComponentController = new map.MapComponentController(); 
  @State latitude:number = 39.9; 
  @State longitude:number = 116.4; 
  aboutToAppear(): void { 
    // 地图初始化参数,设置地图中心点坐标及层级 
    this.mapOption = { 
      position: { 
        target: { 
          latitude: this.latitude, 
          longitude: this.longitude 
        }, 
        zoom: 10 
      } 
    }; 
    // 地图初始化的回调 
    this.callback = async (err, mapController) => { 
      if (!err) { 
        // 获取地图的控制器类,用来操作地图 
        this.mapController = mapController; 
        // 启用我的位置按钮 
        // this.mapController.setMyLocationControlsEnabled(false); 
 
      } 
    }; 
 
  } 
  build() { 
    Stack() { 
      // 调用MapComponent组件初始化地图 
      MapComponent({ mapOptions: this.mapOption, mapCallback: this.callback }).width('100%').height('100%'); 
      Row(){ 
        //默认参数下,图标、文字、背景都存在 
        LocationButton().onClick((event: ClickEvent, result: LocationButtonOnClickResult)=>{ 
          let requestInfo:geoLocationManager.CurrentLocationRequest = {'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX, 'scenario': geoLocationManager.LocationRequestScenario.UNSET,'maxAccuracy': 0}; 
          let locationChange = (err:BusinessError.BusinessError, location:geoLocationManager.Location):void => { 
            if (err) { 
              console.error('locationChanger: err=' + JSON.stringify(err)); 
            } 
            if (location) { 
              this.latitude =location.latitude 
              this.longitude =location.longitude 
              console.log('locationChanger: location=' + JSON.stringify(location)); 
              let markerOptions: mapCommon.MarkerOptions = { 
                position: { 
                  latitude: this.latitude, 
                  longitude: this.longitude 
                } 
              }; 
              // 新建一个默认的marker图标 
              this.mapController?.addMarker(markerOptions); 
              let target: mapCommon.LatLng = { 
                latitude:this.latitude, 
                longitude: this.longitude 
              }; 
              let cameraPosition: mapCommon.CameraPosition = { 
                target: target, 
                zoom: 10 
              }; 
              // 新建CameraUpdate对象 
              let cameraUpdate: map.CameraUpdate = map.newCameraPosition(cameraPosition); 
              // 移动相机 
              this.mapController.moveCamera(cameraUpdate); 
            } 
          }; 
          geoLocationManager.getCurrentLocation(requestInfo, locationChange); 
        }) 
      } 
    }.height('100%') 
 
  } 
}
分享
微博
QQ
微信
回复
2024-08-14 21:14:58
相关问题
如何查看cookie保存位置
485浏览 • 1回复 待解决
HarmonyOS Webview有关位置管理示例
70浏览 • 1回复 待解决
app如何申请位置权限?
131浏览 • 1回复 待解决
HarmonyOS使用Web组件如何监听滚动位置
129浏览 • 1回复 待解决
java中如何设置CommonDialog位置?
5033浏览 • 1回复 待解决
Web组件如何申请位置权限?
140浏览 • 1回复 待解决
Scroll子组件展示位置如何调整
2013浏览 • 1回复 待解决
HarmonyOS申请用户位置权限问题
134浏览 • 1回复 待解决
鸿蒙如何调整组件位置
7380浏览 • 1回复 待解决
如何获取元素位置和大小
1889浏览 • 1回复 待解决
HarmonyOS怎么调整Radio选项框位置
111浏览 • 1回复 待解决
如何设置自定义弹窗位置
1728浏览 • 1回复 待解决
Swiper组件如何设置导航点位置
1748浏览 • 1回复 待解决
Web中网页如何申请位置权限?
128浏览 • 1回复 待解决
MySQL 如何按照地理位置排序?
2529浏览 • 1回复 待解决