目前该按钮是无法更改位置的,你可以尝试自己写个组件传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%') } }
目前该按钮是无法更改位置的,你可以尝试自己写个组件传mapController进去,具体demo如下: