根據(jù)項目需要 要添加導(dǎo)航功能 由于我的app 的內(nèi)部已經(jīng)做了地圖和路線規(guī)劃 同時為了使用用戶的使用習(xí)慣 我選擇了第三方導(dǎo)航 選擇用戶已有地圖軟件進(jìn)行路線規(guī)劃和導(dǎo)航
我選擇的是目前的主流地圖app平臺 高德 百度 騰訊 三種導(dǎo)航軟件 至于google 地圖 由于某些原因 我放棄掉了
說一下我的思路
先檢索手機是否安裝了目標(biāo)地圖app(高德暮顺,百度,騰訊) 如果有安裝其中之一 提示用戶跳轉(zhuǎn)打開地圖 如果沒有安裝 提示安裝
看一下實現(xiàn)吧 相對來說很簡單 其他的就不多說了 直接上代碼
好了 為了方便大家 我把三個app 的Android技術(shù)文檔連接放在這里 供大家參考
高德navi 文檔連接 https://lbs.amap.com/api/amap-mobile/guide/android/route
tencent 文檔連接 https://lbs.qq.com/uri_v1/guide-route.html
baidu 文檔連接 http://lbsyun.baidu.com/index.php?title=uri/api/android
看了這些 思路應(yīng)該更加清楚了
下面是我編寫的工具類
// 檢索地圖軟件
public static boolean isAvilible(Context context, String packageName){
//獲取packagemanager
final PackageManager packageManager = context.getPackageManager();
//獲取所有已安裝程序的包信息
List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
//用于存儲所有已安裝程序的包名
List<String> packageNames = new ArrayList<String>();
//從pinfo中將包名字逐一取出秀存,壓入pName list中
if(packageInfos != null){
for(int i = 0; i < packageInfos.size(); i++){
String packName = packageInfos.get(i).packageName;
packageNames.add(packName);
}
}
//判斷packageNames中是否有目標(biāo)程序的包名捶码,有TRUE,沒有FALSE
return packageNames.contains(packageName);
}
/**
* 指定地圖
*百度地圖包名:com.baidu.BaiduMap
高德地圖包名:com.autonavi.minimap
騰訊地圖包名:com.tencent.map
谷歌地圖 com.google.android.apps.maps
*
*/
public List<String> mapsList (){
List<String> maps = new ArrayList<>();
maps.add("com.baidu.BaiduMap");
maps.add("com.autonavi.minimap");
maps.add("com.tencent.map");
return maps;
}
// 檢索篩選后返回
public List<String> hasMap (Context context){
List<String> mapsList = mapsList();
List<String> backList = new ArrayList<>();
for (int i = 0; i < mapsList.size(); i++) {
boolean avilible = isAvilible(context, mapsList.get(i));
if (avilible){
backList.add(mapsList.get(i));
}
}
return backList;
}
選擇窗口 并實現(xiàn)喚醒目標(biāo)app進(jìn)行導(dǎo)航
public class MapToast {
public void showChooseMap(Context context, View rootView,HisLocationBean bean){
CommonPopupWindow popupWindow=new CommonPopupWindow.Builder(context)
.setView(R.layout.map_toast)
.setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
.setBackGroundLevel(0.5f)
.setViewOnclickListener((view, layoutResId, popupWindow1) -> {
View map_toast_cancelbt = view.findViewById(R.id.map_toast_cancelbt);
View map_toast_gaodebt = view.findViewById(R.id.map_toast_gaodebt);
View map_toast_baidubt = view.findViewById(R.id.map_toast_baidubt);
View map_toast_tencentbt = view.findViewById(R.id.map_toast_tencentbt);
View map_toast_hinttv = view.findViewById(R.id.map_toast_hinttv);
List<String> hasMap = new ThridMapUtil().hasMap(context);
for (int i = 0; i < hasMap.size(); i++) {
if (hasMap.get(i).contains("com.autonavi.minimap")){
map_toast_gaodebt.setVisibility(View.VISIBLE);
}else if (hasMap.get(i).contains("com.baidu.BaiduMap")){
map_toast_baidubt.setVisibility(View.VISIBLE);
}else if (hasMap.get(i).contains("com.tencent.map")){
map_toast_tencentbt.setVisibility(View.VISIBLE);
}
}
if (hasMap.size() == 0){
map_toast_hinttv.setVisibility(View.VISIBLE);
}
map_toast_cancelbt.setOnClickListener(view1 -> {
popupWindow1.dismiss();
});
map_toast_gaodebt.setOnClickListener(view1 -> {
toGaodeNavi(context,bean);
popupWindow1.dismiss();
});
map_toast_baidubt.setOnClickListener(view1 -> {
toBaidu(context,bean);
popupWindow1.dismiss();
});
map_toast_tencentbt.setOnClickListener(view1 -> {
toTencent(context,bean);
popupWindow1.dismiss();
});
}).setOutsideTouchable(true)
.create();
popupWindow.showAtLocation(rootView, Gravity.BOTTOM,0,0);
}
// 百度地圖
public void toBaidu(Context context, HisLocationBean bean){
Intent intent= new Intent("android.intent.action.VIEW", android.net.Uri.parse("baidumap://map/geocoder?location=" + bean.getLat() + "," + bean.getLon()));
context.startActivity(naviIntent);
}
// 高德地圖
public void toGaodeNavi(Context context,HisLocationBean bean){
Intent intent= new Intent("android.intent.action.VIEW", android.net.Uri.parse("androidamap://route?sourceApplication=appName&slat=&slon=&sname=我的位置&dlat="+ bean.getLat() +"&dlon="+ bean.getLon()+"&dname=目的地&dev=0&t=2"));
context.startActivity(naviIntent);
}
// 騰訊地圖
public void toTencent(Context context,HisLocationBean bean){
Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse("qqmap://map/routeplan?type=drive&from=&fromcoord=&to=目的地&tocoord=" + bean.getLat() + "," + bean.getLon() + "&policy=0&referer=appName"));
context.startActivity(naviIntent);
}
}
window的xml文件
<LinearLayout
android:background="@color/color_f6f7f8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_20"
android:layout_marginLeft="@dimen/dp_20"
android:layout_marginRight="@dimen/dp_20"
android:layout_marginBottom="@dimen/dp_20"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView
android:background="@color/color_FFFFFF"
android:layout_marginTop="@dimen/dp_15"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_45"
android:text="高德地圖"
android:visibility="gone"
android:clickable="true"
android:id="@+id/map_toast_gaodebt"
android:textSize="@dimen/titlesise"
android:textColor="@color/color_626262"
android:gravity="center" />
<TextView
android:background="@color/color_FFFFFF"
android:layout_marginTop="@dimen/dp_1"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_45"
android:text="百度地圖"
android:visibility="gone"
android:clickable="true"
android:id="@+id/map_toast_baidubt"
android:textSize="@dimen/titlesise"
android:textColor="@color/color_626262"
android:gravity="center" />
<TextView
android:background="@color/color_FFFFFF"
android:layout_marginTop="@dimen/dp_1"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_45"
android:text="騰訊地圖"
android:visibility="gone"
android:clickable="true"
android:id="@+id/map_toast_tencentbt"
android:textSize="@dimen/titlesise"
android:textColor="@color/color_626262"
android:gravity="center" />
<TextView
android:background="@color/color_FFFFFF"
android:layout_marginTop="@dimen/dp_1"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_45"
android:text="無法檢索地圖軟件或链,請安裝地圖軟件后再試"
android:visibility="gone"
android:clickable="true"
android:id="@+id/map_toast_hinttv"
android:textSize="@dimen/titlesise"
android:textColor="@color/color_626262"
android:gravity="center" />
<TextView
android:background="@color/color_FFFFFF"
android:layout_marginTop="@dimen/dp_15"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_40"
android:text="返回"
android:clickable="true"
android:id="@+id/map_toast_cancelbt"
android:textSize="@dimen/titlesise"
android:textColor="@color/color_626262"
android:gravity="center" />
</LinearLayout>
說在最后 文中提供了google 的包名 https://developers.google.cn/maps/documentation/ 這里是google 技術(shù)文檔 感興趣朋友可以看一下