在集成地图场景的应用中,通常有这样一种场景:需要获取手指触摸点对应地图上的经纬度坐标。获取经纬度坐标后,再进行相关业务处理,譬如放置标记、逆地理编码展示详细地址、查看附近商店等。那么,如何将手指触摸点转换为对应地图上的经纬度坐标?
import { map, mapCommon, MapComponent } from '@kit.MapKit';
import { AsyncCallback } from '@kit.BasicServicesKit';
@Entry
@Component
struct FromScreenLocationDemo {
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('on-mapLoad');
};
this.mapEventManager.on('mapLoad', callback);
}
};
}
build() {
Stack() {
Column() {
Text('This is a Text').fontSize(40).backgroundColor(Color.Green).width('100%').height('10%');
Stack() {
MapComponent({ mapOptions: this.mapOptions, mapCallback: this.callback })
.width('100%')
.height('100%');
}
.width('100%')
.height('100%')
.onTouch((event) => {
// 获取手指触摸位置对应的屏幕像素点
let positionX = this.getUIContext().vp2px(event.touches[0].x);
let positionY = this.getUIContext().vp2px(event.touches[0].y);
let projection: map.Projection | undefined = this.mapController?.getProjection();
if (projection) {
let point: mapCommon.MapPoint = {
positionX: positionX,
positionY: positionY
};
// 转为地图的经纬度坐标
let latLng: mapCommon.LatLng = projection.fromScreenLocation(point);
this.getUIContext().getPromptAction().showToast({
message: 'longitude: ' + latLng.longitude + ' latitude:' + latLng.latitude,
duration: 3000
});
}
});
}.width('100%');
}.height('100%');
}
}