HarmonyOS原子化服务开发实战-地图导航 原创

鸿蒙时代
发布于 2021-12-10 14:48
浏览
0收藏

最近尝试了在开发的项目上添加打开三方应用的地图导航功能,下面是体验的详细代码。


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);
    }
}

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.

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

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
HarmonyOS原子化服务开发实战-地图导航.docx 15.91K 31次下载
收藏
回复
举报
回复
    相关推荐