HarmonyOS 鸿蒙Next中应用如何跳转至百度的地图软件中并进行导航

HarmonyOS 鸿蒙Next中应用如何跳转至百度的地图软件中并进行导航 我想做一款软件,给百度地图传地点位置,然后唤起百度地图并进行导航,请问如何实现,我知道如何唤起系统的地图,但百度地图如何唤起?有没有做过这块功能的同学,麻烦指导下

8 回复

楼主先声明百度地图的跳转协议—

"abilities": [
  {
    // ...其他配置
    "skills": [
      {
        "actions": ["ohos.want.action.viewData"],
        "uris": [
          {
            "scheme": "baidumap", // 百度地图Scheme标识
            "path": "map/direction"
          }
        ]
      }
    ]
  }
]

通过URI参数传递目的地经纬度及导航模式—

let destinationLat = 39.908823; // 目标纬度(需转换为百度坐标系BD09)
let destinationLng = 116.397470; // 目标经度
let address = "北京故宫"; // 目标地点名称

let uri = `baidumap://map/direction?` +
  `destination=latlng:${destinationLat},${destinationLng}|name:${address}&mode=driving`;

通过startAbility接口唤起百度地图—

import { common, Want } from '@kit.AbilityKit';

async startBaiduMapNavigation() {
  let want: Want = {
    uri: uri
  };
  const context = getContext() as common.UIAbilityContext;
  
  try {
    await context.startAbility(want);
  } catch (err) {
    console.error(`跳转失败: ${err.code}, ${err.message}`);
  }
}

更多关于HarmonyOS 鸿蒙Next中应用如何跳转至百度的地图软件中并进行导航的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


感谢大家,我已经找到实现方案了,可以参考百度web的技术文档[https://lbs.baidu.com/faq/api?title=webapi/uri/harmony]

需要用到openLink使用Deep Linking实现应用间跳转-拉起指定应用-应用间跳转-Stage模型开发指导-Ability Kit(程序框架服务)-应用框架 - 华为HarmonyOS开发者

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
@Component
struct Index {
  build() {
    Button('start link', { type: ButtonType.Capsule, stateEffect: true })
      .width('87%')
      .height('5%')
      .margin({ bottom: '12vp' })
      .onClick(() => {
        let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
        let link: string = "link://www.example.com";
        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}`);
        }
      })
  }
}

楼主试下这个: 我是使用openLink来拉起处理的,下面示例 我试了一下是能正常拉起并导航的

Button('拉起百度地图').onClick((event: ClickEvent) => {
  this.openBaiduMap('baidumap://map/direction?destination=latlng:23.1085,113.3196|name:广州塔&mode=driving&coord_type=bd09ll')
})
async openBaiduMap(url: string) {
  let scheme = url.replace('scheme://', '')
  let context = getContext() as common.UIAbilityContext
  let openLinkOptions: OpenLinkOptions = {
    appLinkingOnly: false,
    parameters: { demo_key: 'demo_value' }
  }

  try {
    context.openLink(scheme, openLinkOptions, (err, result) => {
      LogUtil.e(Tag, `openLink callback error.code: ${JSON.stringify(err)}`)
      LogUtil.i(Tag, `openLink callback result: ${JSON.stringify(result.resultCode)}`)
      LogUtil.i(Tag, `openLink callback result data: ${JSON.stringify(result.want)}`)
    }).then(() => {
      LogUtil.i(Tag, `open link success.`)
      return true
    }).catch((err: BusinessError) => {
      // WebOpenApplication.openAppStore('com.alipay.mobile.client')
      LogUtil.e(Tag, `open link failed`, err)
    })
  } catch (e) {
    LogUtil.e(Tag, `exception occured, errCode ${JSON.stringify(e.code)}`)
  }
}

参数说明:

参数名 含义
destination 目的地,支持坐标(latlng)或地址文本
mode 导航方式:driving(驾车)、transit(公交)、walking(步行)等
coord_type 坐标类型,bd09ll 表示百度经纬度坐标系
元服务不能拉应用。楼主是元服务的话不拉不起来的。

根据官方的文档,使用openLink实现应用跳转,demo URL 为

baidumap://map/direction?origin=中关村&destination=五道口&mode=driving&region=北京&src=ios.baidu.openAPIdemo
//本示例是通过该URL启动地图app并进入北京市从中关村到五道口的驾车导航路线图

具体的实现代码为

/**参数为目标经纬度和地址名称*/
jumpToBaidu(lng: string, lat: string, addressName: string){
    let uri = 'baidumap://map/direction?' +
      `destination=latlng:${lat},${lng}|name:${addressName}&mode=driving`
    let want: Want = { uri: uri }
    const content = getContext(this) as common.UIAbilityContext
    content.startAbility(want, (err: BusinessError) => {
      if (err.code) {
        hilog.debug(1000, 'test', 'err=' + err)
      }
    })
}

在HarmonyOS Next中,应用可以通过Want显式跳转方式调用百度地图导航。首先确认设备已安装百度地图应用。使用以下代码示例实现跳转:

let wantInfo = {
    bundleName: "com.baidu.BaiduMap",
    abilityName: "com.baidu.BaiduMap.WelcomeScreen",
    parameters: {
        mode: "navi",
        destination: "39.9087,116.3975", // 目标经纬度
        origin: "39.9908,116.4818" // 起点经纬度(可选)
    }
};
featureAbility.startAbility(wantInfo)

需要声明权限:ohos.permission.START_ABILITIES_NORMAL。参数格式需遵循百度地图的URI规范。

在HarmonyOS Next中实现应用跳转至百度地图并导航,可以通过以下方式实现:

  1. 使用显式Intent跳转: 首先需要确认百度地图的包名和Activity路径(通常为com.baidu.BaiduMap),然后构建Intent进行跳转。

  2. 关键代码示例:

// 构建位置参数
String uri = "baidumap://map/direction?origin=latlng:39.98871,116.43234|name:起点"
    + "&destination=latlng:39.98871,116.53234|name:终点"
    + "&mode=driving";

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.baidu.BaiduMap"); // 指定百度地图包名
startAbility(intent); // 启动百度地图
  1. 参数说明:
  • origin:起点坐标和名称
  • destination:终点坐标和名称
  • mode:导航模式(driving驾车/walking步行/transit公交)
  1. 注意事项:
  • 需要先在设备上安装百度地图APP
  • 建议添加try-catch处理未安装百度地图的情况
  • 坐标使用WGS84坐标系
  1. 备选方案: 如果百度地图未安装,可以捕获异常后跳转系统地图:
try {
    // 百度地图跳转代码
} catch (Exception e) {
    // 跳转系统地图
    String sysUri = "geo:39.98871,116.43234?q=目的地";
    Intent sysIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(sysUri));
    startAbility(sysIntent);
}

建议测试不同版本的百度地图兼容性,部分旧版本可能不支持某些参数。

回到顶部