HarmonyOS 鸿蒙Next 根据提供的经纬度地址信息使用三方地图App进行导航,如果没有安装 App跳转到应用商店进行下载

HarmonyOS 鸿蒙Next 根据提供的经纬度地址信息使用三方地图App进行导航,如果没有安装 App跳转到应用商店进行下载 鸿蒙应用开发中,根据提供的经纬度、地址信息使用百度地图 App、高德地图、谷歌地图进行导航,如果没有安装 App 跳转到应用商店进行下载

3 回复

您好,可以利用API 12新增的方法canOpenLink可以判断是否安装了某个APP。

// src/main/module.json5
"module": {
    "querySchemes": [
      "alipays",
      "amapuri",
      "zhihu",
    ],

参考代码:

import bundleManager from '@ohos.bundle.bundleManager';
import { BusinessError } from '@ohos.base';
import hilog from '@ohos.hilog';
import { promptAction } from '@kit.ArkUI';

@Entry
@Component
struct DocumentListPage {
  arr: ESObject[] = [
    {
      appName: '支付宝',
      link: 'alipays://',
    },
    {
      appName: '高德地图',
      link: 'amapuri://',
    },
    {
      appName: '知乎',
      link: 'zhihu://',
    },
  ]

  checkApp(link: string, appName: string) {
    try {
      let data = bundleManager.canOpenLink(link);
      hilog.info(0x0000, 'testTag', 'canOpenLink successfully: %{public}s', JSON.stringify(data));
      if (data) {
        promptAction.showToast({
          message: `${appName} APP 已安装`,
          duration: 2000,
          bottom: '500lpx'
        });
      } else {
        promptAction.showToast({
          message: `${appName} APP 未安装`,
          duration: 2000,
          bottom: '500lpx'
        });
      }
    } catch (err) {
      if (err['code'] == 17700056) {
        promptAction.showToast({
          message: '请在module.json5配置link,参考注释 querySchemes',
          duration: 2000,
          bottom: '500lpx'
        });
      } else {
        promptAction.showToast({
          message: '未知异常',
          duration: 2000,
          bottom: '500lpx'
        });
      }
      let message = (err as BusinessError).message;
      hilog.error(0x0000, 'testTag', 'canOpenLink failed: %{public}s', message);
    }
  }

  build() {
    Row() {
      Column({ space: 10 }) {
        ForEach(this.arr, (item: ESObject, index: number) => {
          Button(`检查当前手机是否安装了 ${item['appName']} APP`)// 添加跳转功能
            .onClick(() => {
              this.checkApp(item['link'], item['appName'])
            })
        })
      }
    }
  }
}

  • 以上是初步分析结论,如有疑问可以展开回复,看到后会继续协助定位阻碍点。
  • 开源网站上收录了UI、系统接口、Web、创新特性等场景化鸿蒙示例DEMO,开发中可以参考:https://gitee.com/scenario-samples/demo-index

更多关于HarmonyOS 鸿蒙Next 根据提供的经纬度地址信息使用三方地图App进行导航,如果没有安装 App跳转到应用商店进行下载的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


可以通过这个方法:bundleManager.canOpenLink判断应用是否安装

https://developer.huawei.com/consumer/cn/forum/topic/0201154191527432018?fid=0109140870620153026

在HarmonyOS鸿蒙Next中,若需根据经纬度地址信息使用第三方地图App进行导航,并在未安装该App时跳转至应用商店下载,可通过以下步骤实现:

  1. 获取经纬度信息:首先,确保已获取目标位置的经纬度坐标。

  2. 创建Intent:使用HarmonyOS的Intent类,设置ACTION_VIEW操作,并传入经纬度信息。示例代码如下:

    Uri geoUri = Uri.parse("geo:" + latitude + "," + longitude + "?q=" + latitude + "," + longitude + "(Label+Name)");
    Intent intent = new Intent(Intent.ACTION_VIEW, geoUri);
    
  3. 检查App是否安装:通过PackageManager检查设备上是否已安装目标地图App。若未安装,则创建跳转到应用商店的Intent。

  4. 执行Intent:调用startActivity()执行相应的Intent。若未安装地图App,则跳转到应用商店进行下载。

  5. 异常处理:若设备上无支持该Intent的应用,需进行异常处理,避免应用崩溃。

try {
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    // 跳转到应用商店
    Uri storeUri = Uri.parse("market://details?id=com.example.mapapp");
    Intent storeIntent = new Intent(Intent.ACTION_VIEW, storeUri);
    startActivity(storeIntent);
}
回到顶部