参数 | 类型 | 说明 |
|---|---|---|
anchorU | number | 锚点的水平坐标,以图像宽度的比例,建议取值[0, 1],默认值为0.5,异常值按默认值处理。 |
anchorV | number | 锚点的垂直坐标,以图像高度的比例,建议取值[0, 1],默认值为1,异常值按默认值处理。 |
let markerOptions: mapCommon.MarkerOptions = {
position: {
latitude: 39.9,
longitude: 116.4
},
icon: $r('app.media.icon'),
};
this.mapController?.addMarker(markerOptions); 默认的标记参数,anchorV的值为1,以图片下边沿中心作为锚点中心,在缩放地图时,标记图片大小不变的情况下,图片中心点在地图上的位置就会发生变化,图片越大(原问题中图片圆点周围留白越多),现象越明显。需要将标记锚点的中心设置成图片的中心,即可解决问题。
参考代码,将标记参数中的anchorV参数修改为0.5,以图片中心点作为锚点中心。
import { MapComponent, mapCommon, map } from '@kit.MapKit';
import { AsyncCallback } from '@kit.BasicServicesKit';
@Entry
@Component
struct MapPage {
private TAG = "HuaweiMapDemo";
private mapOptions?: mapCommon.MapOptions;
private callback?: AsyncCallback<map.MapComponentController>;
private mapController?: map.MapComponentController;
private mapEventManager?: map.MapEventManager;
aboutToAppear(): void {
// 地图初始化参数,设置地图中心点坐标及层级
this.mapOptions = {
position: {
target: {
latitude: 39.9,
longitude: 116.4
},
zoom: 10
}
};
// 地图初始化的回调
this.callback = async (err, mapController) => {
if (!err) {
// 获取地图的控制器类,用来操作地图
this.mapController = mapController;
this.mapEventManager = this.mapController.getEventManager();
let callback = () => {
console.info(this.TAG, `on-mapLoad`);
};
this.mapEventManager.on("mapLoad", callback);
let markerOptions: mapCommon.MarkerOptions = {
position: {
latitude: 39.9,
longitude: 116.4
},
icon: $r('app.media.icon'),
anchorU: 0.5,
anchorV: 0.5
};
this.mapController?.addMarker(markerOptions);
}
};
}
build() {
Stack() {
// 调用MapComponent组件初始化地图
MapComponent({ mapOptions: this.mapOptions, mapCallback: this.callback }).width('100%').height('100%')
}.height('100%')
}
}