HarmonyOS鸿蒙Next原子化服务开发实战-地图导航

HarmonyOS鸿蒙Next原子化服务开发实战-地图导航 最近尝试了在开发的项目上添加打开三方应用的地图导航功能,下面是体验的详细代码。

package com.jltf.jltf_navigation.slice;

import com.jltf.jltf_navigation.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.bundle.IBundleManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

public class MainAbilitySlice extends AbilitySlice {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP,0x0,"地图导航");
    String address="北京市";
    Uri uri;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        findComponentById(ResourceTable.Id_Nabtn).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                TextField textField = findComponentById(ResourceTable.Id_text);
                address=textField.getText();
                HiLog.info(TAG,"输入地址:"+address);
                if (isAppExist("com.tencent.map")){
                    // 腾讯地图
                    uri=Uri.parse( "qqmap://map/search?keyword=" + address);
                }else if (isAppExist("com.autonavi.minimap")){
                    // 高德地图
                    uri=Uri.parse("androidamap://keywordNavi?sourceApplication="+getBundleName()+"&keyword="+address+"&style=2");
                }else if (isAppExist("com.baidu.BaiduMap")){
                    // 百度地图
                    uri=Uri.parse("baidumap://map/geocoder?src="+getBundleName()+"&address="+address); 
                }
                Intent intent1=new Intent();
                intent1.setUri(uri);
                startAbility(intent1);
            }
        });
    }

    boolean isAppExist(String appPkg) {
        try {
            IBundleManager manager = this.getBundleManager();
            return manager.isApplicationEnabled(appPkg);
        } catch (IllegalArgumentException e) {
            return false;
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

现在只实现了高德、百度和腾讯三方地图导航打开,后续会继续补充。


更多关于HarmonyOS鸿蒙Next原子化服务开发实战-地图导航的实战教程也可以访问 https://www.itying.com/category-93-b0.html

8 回复

老师你好,请教一个问题:SDK6或更高版本的 JS/ETS ,现在能实现APP内的高德或百度的地图吗,网上查了一圈多数是JAVA版本的(参考截图),谢谢老师

cke_2698.jpeg

更多关于HarmonyOS鸿蒙Next原子化服务开发实战-地图导航的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


应该还没有。

感谢老师,

基本信息

  • 姓名:张三
  • 年龄:20
  • 职业:学生

太厉害了,支持楼主。

好文章写的真不错、学习收藏了

很实用的文章,学到了不少

不错的文章,受益匪浅。

在HarmonyOS鸿蒙Next中开发原子化服务地图导航,首先需集成地图SDK,如高德或百度地图。通过@ohos.geolocation获取用户位置,使用@ohos.router实现页面跳转。创建ServiceAbility处理导航逻辑,利用DataAbility管理数据。通过FormAbility提供卡片服务,用户可快速访问导航功能。确保应用轻量化,符合原子化服务的设计理念,提升用户体验。

回到顶部