【JAVA UI】HarmonyOS如何跳转三方的地图导航
参考资料
高德(https://lbs.amap.com/api/amap-mobile/guide/android/route)
百度(http://lbsyun.baidu.com/index.php?title=uri/api/ios&qq-pf-to=pcqq.c2c)
腾讯(https://lbs.qq.com/uri_v1/guide-mobile-navAndRoute.html)
【JAVA UI】abilitySlice或ability以Action方式跳转
代码实现
-
xml布局绘画
在xml布局绘制三个Text组件分别为“百度地图”,“腾讯地图”,“高德地图”,代码如下
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<Text
ohos:id="$+id:start_baidu1"
ohos:height="100vp"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="百度地图"
ohos:text_size="40vp"
/>
<Text
ohos:id="$+id:start_tongxun"
ohos:height="100vp"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:background_element="#ed6161"
ohos:layout_alignment="horizontal_center"
ohos:text="腾讯地图"
ohos:text_size="40vp"
/>
<Text
ohos:id="$+id:start_gaode"
ohos:height="100vp"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="高德地图"
ohos:text_size="40vp"
/>
</DirectionalLayout>
-
java代码实现
百度地图导航
Intent intent = new Intent(); try { Operation operation = new Intent.OperationBuilder() .withAbilityName("") .withDeviceId("") .withUri(Uri.parse("baidumap://map/navi?location="+lon+","+lat+"&src=andr.baidu.openAPIdemo")) .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT) .build(); intent.setOperation(operation); startAbility(intent); }catch (Exception e){ e.printStackTrace(); }
高德地图
try { Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withAbilityName("") .withDeviceId("") .withAction("android.intent.action.VIEW") .withUri(Uri.parse("amapuri://route/plan/?sid=&slat=39.92848272&slon=116.39560823&sname=A&did=&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&t=0")) .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT) .build(); intent.setOperation(operation); startAbility(intent); }catch (Exception e){ e.printStackTrace(); }
腾讯地图
Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withAbilityName("") .withDeviceId("") .withUri(Uri.parse("qqmap://map/routeplan?type=drive&from=清华&fromcoord=39.994745,116.247282&to=怡和世家&tocoord=39.867192,116.493187&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77")) .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT) .build(); intent.setOperation(operation); startAbility(intent);
全部代码
package com.example.myapplication.slice; import com.example.myapplication.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.aafwk.content.Operation; import ohos.agp.components.Component; import ohos.utils.net.Uri; public class MainAbilitySlice extends AbilitySlice { private float lat=118.803714f; private float lon=32.093512f; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); findComponentById(ResourceTable.Id_start_baidu1).setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { toBaidu(); } }); findComponentById(ResourceTable.Id_start_tongxun).setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { toTencent(); } }); findComponentById(ResourceTable.Id_start_gaode).setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { toGaodeNavi(); } }); } // 百度地图 public void toBaidu(){ Intent intent = new Intent(); try { Operation operation = new Intent.OperationBuilder() .withAbilityName("") .withDeviceId("") .withUri(Uri.parse("baidumap://map/navi?location="+lon+","+lat+"&src=andr.baidu.openAPIdemo")) .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT) .build(); intent.setOperation(operation); startAbility(intent); }catch (Exception e){ e.printStackTrace(); } } // 高德地图 public void toGaodeNavi(){ try { Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withAbilityName("") .withDeviceId("") .withAction("android.intent.action.VIEW") .withUri(Uri.parse("amapuri://route/plan/?sid=&slat=39.92848272&slon=116.39560823&sname=A&did=&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&t=0")) .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT) .build(); intent.setOperation(operation); startAbility(intent); }catch (Exception e){ e.printStackTrace(); } } // 腾讯地图 public void toTencent(){ Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withAbilityName("") .withDeviceId("") .withUri(Uri.parse("qqmap://map/routeplan?type=drive&from=清华&fromcoord=39.994745,116.247282&to=怡和世家&tocoord=39.867192,116.493187&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77")) .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT) .build(); intent.setOperation(operation); startAbility(intent); } }
FAQ
鸿蒙应用跳转到Android应用
Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() //activity 名称 .withAbilityName("xxx.MyTestActivity") //包名 .withBundleName("包名") .withDeviceId("") .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT) .build(); intent.setOperation(operation); startAbility(intent);
鸿蒙跳转微信
Intent intent1=new Intent(); Operation operation=new Intent.OperationBuilder() .withAction("android.intent.action.MAIN") .withBundleName("com.tencent.mm") .withAbilityName("com.tencent.mm.ui.LauncherUI") .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT) .build(); intent1.addEntity("android.intent.category.LAUNCHER"); intent1.setOperation(operation); startAbility(intent1);
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh