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

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

阅读 670
1 个回答

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

可以尝试自己写个组件传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
      }
    };
    // &#22320;&#22270;&#21021;&#22987;&#21270;&#30340;&#22238;&#35843;
    this.callback = async (err, mapController) =&gt; {
      if (!err) {
        // &#33719;&#21462;&#22320;&#22270;&#30340;&#25511;&#21046;&#22120;&#31867;&#65292;&#29992;&#26469;&#25805;&#20316;&#22320;&#22270;
        this.mapController = mapController;
        // &#21551;&#29992;&#25105;&#30340;&#20301;&#32622;&#25353;&#38062;
        // 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%')

  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进