HarmonyOS Next开发教程之地图定位

今天分享一下在鸿蒙开发中的地图定位问题,也就是在地图中如何定位自己所在的位置。
关于如何加载显示地图在之前的文章已经详细介绍过,有问题的友友可以点击查看:
HarmonyOS NEXT实战教程-实现Keep运动轨迹
​​将地图定位到自己所在的位置,有几种方法:
一种是在初始化地图前先获取到当前的坐标,然后将坐标初始化到地图上

“`geoLocationManager.getCurrentLocation(request).then((result) => {
console.log…


This content originally appeared on DEV Community and was authored by wei chang

image.png
今天分享一下在鸿蒙开发中的地图定位问题,也就是在地图中如何定位自己所在的位置。
关于如何加载显示地图在之前的文章已经详细介绍过,有问题的友友可以点击查看:
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方法移动相机来实现:


```// 新建CameraUpdate对象
let cameraUpdate: map.CameraUpdate = map.newCameraPosition(cameraPosition);
// 移动相机
this.mapController.moveCamera(cameraUpdate);

然后可以使用setMyLocation方法标记当前的位置坐标:

//设置我的位置
this.mapController?.setMyLocation(location)
//显示我的位置
this.mapController?.setMyLocationEnabled(true)

image.png
这里有一个困扰了我很久的问题,这里的位置标记点和定位是用的同一个坐标,位置标记的正确说明坐标没有问题,那么既然坐标没有问题的话,为什么我的位置不在屏幕中央,而且就偏了那么一点点?
研究许久之后了解到,其实在初始化地图和移动镜头时不能直接传入获取到的位置坐标,而是要进行坐标系的转换,由WGS84坐标系转换到GCJ02坐标系,在进行地图的定位:

let gcj02Posion: mapCommon.LatLng =  await map.convertCoordinate(mapCommon.CoordinateType.WGS84, mapCommon.CoordinateType.GCJ02,    point);

这样的话当前的位置就会出现在屏幕中间啦。


This content originally appeared on DEV Community and was authored by wei chang


Print Share Comment Cite Upload Translate Updates
APA

wei chang | Sciencx (2025-03-09T00:00:50+00:00) HarmonyOS Next开发教程之地图定位. Retrieved from https://www.scien.cx/2025/03/09/harmonyos-next%e5%bc%80%e5%8f%91%e6%95%99%e7%a8%8b%e4%b9%8b%e5%9c%b0%e5%9b%be%e5%ae%9a%e4%bd%8d/

MLA
" » HarmonyOS Next开发教程之地图定位." wei chang | Sciencx - Sunday March 9, 2025, https://www.scien.cx/2025/03/09/harmonyos-next%e5%bc%80%e5%8f%91%e6%95%99%e7%a8%8b%e4%b9%8b%e5%9c%b0%e5%9b%be%e5%ae%9a%e4%bd%8d/
HARVARD
wei chang | Sciencx Sunday March 9, 2025 » HarmonyOS Next开发教程之地图定位., viewed ,<https://www.scien.cx/2025/03/09/harmonyos-next%e5%bc%80%e5%8f%91%e6%95%99%e7%a8%8b%e4%b9%8b%e5%9c%b0%e5%9b%be%e5%ae%9a%e4%bd%8d/>
VANCOUVER
wei chang | Sciencx - » HarmonyOS Next开发教程之地图定位. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/09/harmonyos-next%e5%bc%80%e5%8f%91%e6%95%99%e7%a8%8b%e4%b9%8b%e5%9c%b0%e5%9b%be%e5%ae%9a%e4%bd%8d/
CHICAGO
" » HarmonyOS Next开发教程之地图定位." wei chang | Sciencx - Accessed . https://www.scien.cx/2025/03/09/harmonyos-next%e5%bc%80%e5%8f%91%e6%95%99%e7%a8%8b%e4%b9%8b%e5%9c%b0%e5%9b%be%e5%ae%9a%e4%bd%8d/
IEEE
" » HarmonyOS Next开发教程之地图定位." wei chang | Sciencx [Online]. Available: https://www.scien.cx/2025/03/09/harmonyos-next%e5%bc%80%e5%8f%91%e6%95%99%e7%a8%8b%e4%b9%8b%e5%9c%b0%e5%9b%be%e5%ae%9a%e4%bd%8d/. [Accessed: ]
rf:citation
» HarmonyOS Next开发教程之地图定位 | wei chang | Sciencx | https://www.scien.cx/2025/03/09/harmonyos-next%e5%bc%80%e5%8f%91%e6%95%99%e7%a8%8b%e4%b9%8b%e5%9c%b0%e5%9b%be%e5%ae%9a%e4%bd%8d/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.