今天分享一下在鸿蒙开发中的地图定位问题,也就是在地图中如何定位自己所在的位置。 关于如何加载显示地图在之前的文章已经详细介绍过,有问题的友友可以点击查看: HarmonyOS NEXT实战教程-实现Keep运动轨迹 将地图定位到自己所在的位置,有几种方法: 一种是在初始化地图前先获取到当前的坐标,然后将坐标初始化到地图上
geoLocationManager.getCurrentLocation(request).then((result)
console.log('current location: ' + JSON.stringify(result));
//初始化地图坐标
this.mapOptions = {
position: {
target: {
latitude: result.latitude,
longitude: result.longitude
},
//当前比例
zoom: 14.2
},
//地图最小比例,也就是能局部放大的小比例
minZoom:3,
//地图最大比例
maxZoom:18,
scaleControlsEnabled: true
};
})
.catch((error:BusinessError) => {
console.error('promise, getCurrentLocation: error=' + JSON.stringify(error));
});
另一种方法是使用moveCamera方法移动相机来实现:
//
let cameraUpdate: map.CameraUpdate = map.newCameraPosition(cameraPosition);
// 移动相机
this.mapController.moveCamera(cameraUpdate);
然后可以使用setMyLocation方法标记当前的位置坐标:
kotlin
//设置我的位置
this.mapController?.setMyLocation(location)
//显示我的位置
this.mapController?.setMyLocationEnabled(true)
这里有一个困扰了我很久的问题,这里的位置标记点和定位是用的同一个坐标,位置标记的正确说明坐标没有问题,那么既然坐标没有问题的话,为什么我的位置不在屏幕中央,而且就偏了那么一点点? 研究许久之后了解到,其实在初始化地图和移动镜头时不能直接传入获取到的位置坐标,而是要进行坐标系的转换,由WGS84坐标系转换到GCJ02坐标系,在进行地图的定位:
ini
let gcj02Posion: mapCommon.LatLng = await map.convertCoordinate(mapCommon.CoordinateType.WGS84, mapCommon.CoordinateType.GCJ02, point);
这样的话当前的位置就会出现在屏幕中间啦。