鸿蒙next开启地图服务

一般手机软件有的都会有开启地图功能,这里说一下怎么开启地图服务

1、 首先你需要配置一些东西,在华为的agc平台上,下边链接就是详细的教程

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/map-config-agc-V5

我说一下你自己需要在ide上做的,

有三个证书,这个需要你从华为官方上下载好的,就是上边链接告诉你要做的一个p12,一个p7b,一个cer,你需要在项目里设置一下

在module.json5里你需要申请地图权

'requestPermissions':[

{

"name": 'ohos.permission.LOCATION',

'reason': 'KaTeX parse error: Expected 'EOF', got '}' at position 139: ... } }̲, { ...string:GPSReason',

'usedScene': {

"abilities": [

'EntryAbility'

],"when": 'inuse'

}

}

]

然后再entryablity里去把权限申请好

let manager= abilityAccessCtrl.createAtManager()

await manager.requestPermissionsFromUser(this.context,[

'ohos.permission.LOCATION',

'ohos.permission.APPROXIMATELY_LOCATION'])//地图权限

}

这样在一进入应用就会提升app需要开启地图权限

显示地图主要是MapComponent组件

你需要一个地图控制器

controller?: map.MapComponentController

一个回调函数

private callback?: AsyncCallback<map.MapComponentController>

你在build函数里写一个地图的组件,

build() {

Stack() {

// 调用MapComponent组件初始化地图

MapComponent({ mapOptions: this.mapOptions, mapCallback: this.callback }).width('100%').height('100%');

}.height('100%')

}

有这么两个参数,mapOptions有很多参数,你只写position就行,一个经度一个纬度,具体是什么数值,我觉得如果符合经纬度的值的可以随便写,比如(116,60)经度116纬度60,

然后是这个回调函数,在aboutToAppear函数里写这个函数执行的逻辑

this.callback = async (err, mapController) => {

if (!err) {

// 获取地图的控制器类,用来操作地图

this.mapController = mapController

}

};

他的回调函数有两个参数,一个err是报错,一个是mapController,用来给我们定义的控制器赋值,这样我们就拿到了地图的控制器。
怎么操作地图知道你自己的位置呢

1、首先你需要获取经纬度

let result = await geoLocationManager.getCurrentLocation()

//2、经纬度转化成街道

let Addressesresult = await geoLocationManager.getAddressesFromLocation(

{

latitude: result.latitude,

longitude: result.longitude

}

)

this.currentMessage = Addressesresult[0].placeName as string

这个currentMessage 就是街道信息后续根据业务可能需要用到

//3、设置当前位置

this.controller.setMyLocation({

latitude: result.latitude,

longitude: result.longitude,

altitude: result.altitude,

accuracy: result.accuracy,

speed: result.speed,

timeStamp: result.timeStamp,

direction: result.direction,

timeSinceBoot: result.timeSinceBoot

})

//4、准备一个相机、位置挪到定位处

let camera: map.CameraUpdate = map.newCameraPosition({

target: {

longitude: result.longitude,

latitude: result.latitude

}, zoom: 16

})

this.controller.moveCamera(camera)

//5、转换坐标84转02

let transposition02: mapCommon.LatLng = await map.convertCoordinate(mapCommon.CoordinateType.WGS84

, mapCommon.CoordinateType.GCJ02, result)

//6、你自己打点看看你在哪里

//自定义打点展示,加一个标记,如果效果不理想们可以自定义builder

this.controller.addMarker({

position: {

latitude: transposition02.latitude,

longitude: transposition02.longitude

},

title: '当前位置',

clickable: true,

//街道信息

snippet: this.currentMessage

})
重要一点模拟器不支持地图!!!! ,目前只有真机可以,这在学习鸿蒙上有一定的难度,就是代码写完不知道行不行,之前写完地图能出来。但是没有街道等信息,只有左下角的petal maps,和右边的加减号可以,其余的都是空白,应该在真机上不出意外应该是没问题的

在aboutToAppear函数里也可以把这两代码加上,模拟器上显示不出来地图但是可以显示出来一个点,那个就是你当前的位置,至于准不准我也不清楚,毕竟模拟器不显示地图(手动狗头)

//设置地图展示位置

this.controller.setMyLocationEnabled(true)

//设置地图控制器

this.controller.setMyLocationControlsEnabled(true)

相关推荐
SuperHeroWu710 小时前
【HarmonyOS】元服务入门详解 (一)
华为·harmonyos·鸿蒙·元服务·卡片·免安装
Georgewu10 小时前
【HarmonyOS】ArkUI-X 跨平台框架入门详解(一)
harmonyos
SuperHeroWu710 小时前
【HarmonyOS】元服务概念详解
华为·harmonyos·鸿蒙·概念·元服务·详解
Georgewu10 小时前
【HarmonyOS】ArkUI-X一套代码跑多端的跨平台方案概念详解
harmonyos
ins_lizhiming14 小时前
【人工智能】华为昇腾NPU-Vllm Ascend部署及镜像制作
人工智能·pytorch·华为·语言模型·自然语言处理
zhanshuo15 小时前
HarmonyOS NEXT 智能场景识别实战:让设备主动思考的关键技术揭秘
harmonyos
zhanshuo19 小时前
HarmonyOS分布式能力全解析:手机一放下,音箱自动响起!
harmonyos
zhanshuo19 小时前
基于HarmonyOS的智能灯光控制系统设计:从定时触发到动作联动全流程实战
harmonyos
weixin_5412999420 小时前
鸿蒙应用开发: 鸿蒙项目中使用私有 npm 插件的完整流程
华为·npm·harmonyos·鸿蒙
Georgewu20 小时前
【HarmonyOS】元服务概念详解
harmonyos