移动应用、浏览器端应用均可调起鸿蒙版百度地图。
实现原理
在百度地图的module.json5配置文件中配置skills标签
"uris": [{"scheme": "baidumap","host": "map"}]
移动应用调用百度地图
该调用方式是指第三方基于Harmony SDK进行应用开发时要调用Harmony版百度地图客户端需遵守的协议。该协议要正确执行除了要参数填写正确外,还必须要安装Harmony百度地图。
使用openLink实现应用跳转。在openLink接口的link字段中传入目标应用的URL信息,并将options字段中的appLinkingOnly配置为false。
示例代码
import { common, OpenLinkOptions } from '@kit.AbilityKit';import { BusinessError } from '@kit.BasicServicesKit';import { hilog } from '@kit.PerformanceAnalysisKit';const TAG: string = '[UIAbilityComponentsOpenLink]';const DOMAIN_NUMBER: number = 0xFF00;@Entry@Componentstruct Index {build() {Button('start link', { type: ButtonType.Capsule, stateEffect: true }).width('87%').height('5%').margin({ bottom: '12vp' }).onClick(() => {let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;let link: string = "baidumap://map";let openLinkOptions: OpenLinkOptions = {appLinkingOnly: false};try {context.openLink(link, openLinkOptions).then(() => {hilog.info(DOMAIN_NUMBER, TAG, 'open link success.');}).catch((err: BusinessError) => {hilog.error(DOMAIN_NUMBER, TAG, `open link failed. Code is ${err.code}, message is ${err.message}`);});} catch (paramError) {hilog.error(DOMAIN_NUMBER, TAG, `Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);}})}}
浏览器调用百度地图
index.html示例代码
<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body><h1>Hello World</h1><!--方式一、通过绑定事件window.open方法实现跳转--><button class="doOpenLink" onclick="doOpenLink()">跳转其他应用一</button><!--方式二、通过超链接实现跳转--><a href="baidumap://map">跳转其他应用二</a></body></html><script>function doOpenLink() {window.open("baidumap://map")}</script>
Web组件调用百度地图
Web组件需要跳转DeepLink链接应用时,可通过拦截回调onLoadIntercept中对定义的事件进行处理,实现应用跳转。
import { webview } from '@kit.ArkWeb';import { BusinessError } from '@kit.BasicServicesKit';import { common } from '@kit.AbilityKit';@Entry@Componentstruct WebComponent {controller: webview.WebviewController = new webview.WebviewController();build() {Column() {Web({ src: $rawfile('index.html'), controller: this.controller }).onLoadIntercept((event) => {const url: string = event.data.getRequestUrl();if (url === 'baidumap://map') {(getContext() as common.UIAbilityContext).openLink(url).then(() => {console.log('openLink success');}).catch((err: BusinessError) => {console.error('openLink failed, err:' + JSON.stringify(err));});return true;}// 返回true表示阻止此次加载,否则允许此次加载return false;})}}}
百度地图Open API每个接口对应一个专门的Uri串,每个串中相应参数会在下面详细说明。
通用参数
每个协议都可以填写的通用参数,其中coord_type指坐标类型参数,默认为bd09经纬度坐标,如用户使用的坐标类型为gcj02、wgs84,则必须传递此参数值(示例:coord_type=gcj02、coord_type=wgs84),否则会导致地点坐标位置偏移。
参数名称 | 类型 | 是否必选 | 参数说明 |
---|---|---|---|
coord_type | string | 可选 | 坐标类型,可选参数,默认为bd09ll百度经纬度坐标。允许的值为bd09ll、bd09mc、gcj02、wgs84。bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托坐标,gcj02表示经过国测局加密的坐标,wgs84表示gps获取的坐标 |
needLocation | string | 可选 | 调用时指定该key为yes或no来决定是否强制需要定位 |
isReducedAccuracyEnough | string | 可选 | 定位调用时指定该key为yes或no来决定,需要定位时低精度是否满足,默认no不满足,这个必须配合needLocation=yes使用 |
popRoot | string | 可选 | 默认OpenAPI跳转要清空UI栈,指定该key为no可以不清空UI栈 |
显示主图区(首页)
协议1:baidumap://map/show
协议2:baidumap://map
参数名称 | 类型 | 是否必选 | 参数说明 | 版本 |
---|---|---|---|---|
bounds | string | 可选 | 图区范围 优先级最高 | 1.0.0 |
zoom | string | 可选 | 图区的level | 1.0.0 |
center | string | 可选 | 图区中心点,lat,lng (先纬度,后经度)=yes使用 | 1.0.0 |
userlocation | string | 可选 | 用定位点做图区重点点 | 1.0.0 |
例1:baidumap://map/show?zoom=18¢er=39.989956,116.323066&coord_type=gcj02&src=push
例2:baidumap://map/show?bounds=4050547,12128734,4051635,12127547&coord_type=bd09mc
标注
调起百度地图时,在主图区的指定坐标点上显示点的名称和内容信息,弹泡泡方式
协议1:baidumap://map/marker
参数名称 | 类型 | 是否必选 | 参数说明 | 版本 |
---|---|---|---|---|
title | string | 必选 | 标注点显示标题 | 1.0.0 |
content | string | 必选 | 标注点显示内容 | 1.0.0 |
location | string | 必选 | 图区中心点,lat,lng (先纬度,后经度) | 1.0.0 |
coord_type | string | 可选 | 坐标类型,可选参数,默认为bd09ll百度经纬度坐标。允许的值为bd09ll、bd09mc、gcj02、wgs84。bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托坐标,gcj02表示经过国测局加密的坐标,wgs84表示gps获取的坐标 | 1.0.0 |
zoom | string | 可选 | 比例尺 | 1.0.0 |
src | string | 必选 | 统计参数 | 1.0.0 |
例:baidumap://map/marker?title=我的位置&location=40.047669,116.313082&content=百度奎科大厦&src=push
反GEO
协议:baidumap://map/geocoder
参数名称 | 类型 | 是否必选 | 参数说明 | 版本 |
---|---|---|---|---|
address | string | 必选 | 地址名称,和location互斥,优先级 address > location | 1.0.0 |
location | string | 必选 | 图区中心点,lat,lng (先纬度,后经度) ,和address互斥,优先级 address > location | 1.0.0 |
name | string | 可选 | 反geo名称 | |
coord_type | string | 可选 | 坐标类型,可选参数,默认为bd09ll百度经纬度坐标。允许的值为bd09ll、bd09mc、gcj02、wgs84。bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托坐标,gcj02表示经过国测局加密的坐标,wgs84表示gps获取的坐标 | 1.0.0 |
zoom | string | 可选 | 比例尺 | 1.0.0 |
src | string | 必选 | 统计参数 | 1.0.0 |
例1:baidumap://map/marker?name=北京市信息路甲9号&location=40.047669,116.313082&src=push
例2:baidumap://map/geocoder?address=北京市海淀区信息路甲9号&src=push
poi详情
协议:baidumap://map/place/detail
参数名称 | 类型 | 是否必选 | 参数说明 | 版本 |
---|---|---|---|---|
uid | string | 必选 | poi的uid | 1.0.0 |
x | string | 可选 | 坐标 | 1.0.0 |
y | string | 可选 | 坐标 | 1.0.0 |
show_type | string | 可选 | 展现方式 1.detail_bar是POI图区详情页(底图带底部bar的页面),2.detail_page是poi详情页。默认值是detail_bar,如果不带show_type参数,用默认值。 | 1.0.0 |
例:baidumap://map/place/detail?uid=8d96925c6ccf855cc1f1cf38&src=push
poi列表页
协议:baidumap://map/place/search
参数名称 | 类型 | 是否必选 | 参数说明 | 版本 |
---|---|---|---|---|
query | string | 必选 | 搜索关键词 | 1.0.0 |
例:baidumap://map/place/search?query=KTV&src=push&location=39.989813,116.314094&radius=1000
两框页
协议:baidumap://map/routepage
参数名称 | 类型 | 是否必选 | 参数说明 | 版本 |
---|---|---|---|---|
type | string | 必选 | 路线类型,string型 walk步行、ride行、car驾车、neweng新能源、truck货车、motor摩托车、bus公交、train火车、flight飞机、coach大巴、aitravel、rentcar_entrance 打车 | 1.0.0 |
param | string | 额外参数 | 1.0.0 |
例1:baidumap://map/routepage?type=walk¶m={"mode":"walking","fr":"test"}
例2:baidumap://map/routepage?popRoot=no&needCloud=yes&type=walk&src=walk.activity.widget
路线页
协议:baidumap://map/direction
参数名称 | 类型 | 是否必选 | 参数说明 | 版本 |
---|---|---|---|---|
origin | string | 必选 | 起点名称或经纬度,或者可同时提供名称和经纬度,此时经纬度优先级高,将作为导航依据,名称只负责展示。建筑ID、楼层ID用于室内openAPI,为可选字段 支持参数格式 1、名称:天安门 2、经纬度(先纬度,后经度):39.98871,116.43234 3、名称和经纬度(中间有一条竖线分割):name:天安门|latlng:39.98871,116.43234 注意:仅有名称的情况下,请不要带“name:”,只需要origin=“起点名称” (参数值需要进行encode) | 1.0.0 |
destination | string | 必选 | 同上 | 1.0.0 |
originName | string | 可选 | 起点名称 (参数值需要进行encode) | 1.0.0 |
originAddr | string | 可选 | 起点地址 (参数值需要进行encode) | 1.0.0 |
destName | string | 可选 | 终点名称 (参数值需要进行encode) | 1.0.0 |
destAddr | string | 可选 | 终点地址 (参数值需要进行encode) | 1.0.0 |
mode | string | 可选 | 出行方式,固定为transit、driving | 1.0.0 |
force_mode | string | 可选 | 优先级高于mode, 固定为recommend,driving,transit | 1.0.0 |
例:baidumap://map/direction?popRoot=no&src=duhelper&pageflag=4&origin=%E6%88%91%E7%9A%84%E4%BD%8D%E7%BD%AE&destination=name%3A%E8%B1%86%E5%90%84%E5%BA%84%E6%96%B0%E6%9D%91%7Clatlng%3A40.122275%2C116.312750&mode=transit&destination_uid=d6df91c1b2e8f26af7b91665
导航页
协议:baidumap://map/navi
参数名称 | 类型 | 是否必选 | 参数说明 | 版本 |
---|---|---|---|---|
location | string | 必选 | 终点坐标(经纬度),纬度在前,经度在后,使用“,”分割,例如:22.615108,114.035529 | 1.0.0 |
uid | string | 非必选,但不传不保证进导航的算路一定成功 | 终点uid | 1.0.0 |
目的地:百度大厦
baidumap://map/navi?location=12947474.59,4846476.50998&uid=435d7aea036e54355abbbcc8&coord_type=bd09mc
目的地:深圳北站
baidumap://map/navi?location=22.615108,114.035529&uid=6f6241e3c05ab1a093114c5e&coord_type=bd09ll
上一篇
下一篇
本篇文章对您是否有帮助?