HarmonyOS Next开发教程之地图定位

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

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

相关推荐
bestadc9 小时前
鸿蒙 所有API缩略图鉴
harmonyos
马剑威(威哥爱编程)12 小时前
HarmonyOS 5.0 分布式数据协同与跨设备同步
分布式·华为·harmonyos·arkts·harmonyos-next
茄子忍者烧纸尿裤21 小时前
鸿蒙开发——4.ArkTS快速入门指南
华为·harmonyos
_waylau1 天前
【HarmonyOS NEXT+AI】问答05:ArkTS和仓颉编程语言怎么选?
人工智能·华为·harmonyos·arkts·鸿蒙·仓颉
鸿蒙布道师1 天前
鸿蒙NEXT开发动画案例3
android·ios·华为·harmonyos·鸿蒙系统·arkui·huawei
雪芽蓝域zzs2 天前
HarmonyOS开发-组件市场
华为·harmonyos
__Benco2 天前
OpenHarmony平台驱动开发(十),MMC
人工智能·驱动开发·harmonyos
何玺2 天前
鸿蒙电脑:五年铸剑开新篇,国产操作系统新引擎
华为·电脑·harmonyos
__Benco2 天前
OpenHarmony平台驱动开发(十一),PIN
人工智能·驱动开发·harmonyos