Android仿微信調(diào)用第三方地圖應(yīng)用導(dǎo)航(高德、百度、騰訊)
實(shí)現(xiàn)目標(biāo)
先來(lái)一張微信功能截圖看看要做什么

其實(shí)就是有一個(gè)目的地,點(diǎn)擊目的地的時(shí)候彈出可選擇的應(yīng)用進(jìn)行導(dǎo)航。
大腦動(dòng)一下,要實(shí)現(xiàn)這個(gè)功能應(yīng)該大體分成兩步:
- 底部彈出可選的地圖菜單進(jìn)行展示
- 點(diǎn)擊具體菜單某一項(xiàng)的時(shí)候調(diào)用對(duì)應(yīng)地圖的api進(jìn)行導(dǎo)航就ok啦
底部菜單這里用PopupWindow來(lái)做。
實(shí)現(xiàn)
1、菜單顯示
PopupWindow支持傳入view進(jìn)行彈出展示,所有我們直接寫(xiě)一個(gè)菜單布局,高德、百度、騰訊 再加一個(gè)取消。
map_navagation_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/baidu_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ulz_white_selector"
android:text="百度地圖"/>
<include layout="@layout/common_line_view"/>
<Button
android:id="@+id/gaode_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ulz_white_selector"
android:text="高德地圖"/>
<include layout="@layout/common_line_view"/>
<Button
android:id="@+id/tencent_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ulz_white_selector"
android:text="騰訊地圖"/>
<include layout="@layout/common_line_view"/>
<Button
android:id="@+id/cancel_btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ulz_white_selector"
android:text="取消"/>
</LinearLayout>
這里為了顯示效果,自己寫(xiě)了個(gè)PopupWindow的子類(lèi),一般你直接用PopupWindow就可以了。
然后在需要調(diào)用的地方顯示PopupWindow
mapSheetView = LayoutInflater.from(this).inflate(R.layout.map_navagation_sheet, null);
mBottomSheetPop = new BottomSheetPop(this);
mBottomSheetPop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
mBottomSheetPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
mBottomSheetPop.setContentView(mapSheetView);
mBottomSheetPop.setBackgroundDrawable(new ColorDrawable(0x00000000));
mBottomSheetPop.setOutsideTouchable(true);
mBottomSheetPop.setFocusable(true);
mBottomSheetPop.showAtLocation(this.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
2、點(diǎn)擊每個(gè)菜單調(diào)用對(duì)用地圖的導(dǎo)航api
這個(gè)每個(gè)地圖的官網(wǎng)都會(huì)有介紹,你只需要把目的地名稱(chēng),經(jīng)緯度信息傳過(guò)去就好了,沒(méi)什么多說(shuō)的,直接貼代碼。
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.navigation_btn:
mBottomSheetPop = new BottomSheetPop(this);
mBottomSheetPop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
mBottomSheetPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
mBottomSheetPop.setContentView(mapSheetView);
mBottomSheetPop.setBackgroundDrawable(new ColorDrawable(0x00000000));
mBottomSheetPop.setOutsideTouchable(true);
mBottomSheetPop.setFocusable(true);
mBottomSheetPop.showAtLocation(this.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
break;
case R.id.cancel_btn2:
if (mBottomSheetPop != null) {
mBottomSheetPop.dismiss();
}
break;
case R.id.baidu_btn:
if (isAvilible(this, "com.baidu.BaiduMap")) {//傳入指定應(yīng)用包名
try {
Intent intent = Intent.getIntent("intent://map/direction?" +
"destination=latlng:" + mInfo.getLat() + "," + mInfo.getLng() + "|name:我的目的地" + //終點(diǎn)
"&mode=driving&" + //導(dǎo)航路線方式
"&src=appname#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
startActivity(intent); //啟動(dòng)調(diào)用
} catch (URISyntaxException e) {
Log.e("intent", e.getMessage());
}
} else {//未安裝
//market為路徑,id為包名
//顯示手機(jī)上所有的market商店
Toast.makeText(this, "您尚未安裝百度地圖", Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}
}
mBottomSheetPop.dismiss();
break;
case R.id.gaode_btn:
if (isAvilible(this, "com.autonavi.minimap")) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
//將功能Scheme以URI的方式傳入data
Uri uri = Uri.parse("androidamap://navi?sourceApplication=appname&poiname=fangheng&lat=" + mInfo.getLat() + "&lon=" + mInfo.getLng() + "&dev=1&style=2");
intent.setData(uri);
//啟動(dòng)該頁(yè)面即可
startActivity(intent);
} else {
Toast.makeText(this, "您尚未安裝高德地圖", Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}
}
mBottomSheetPop.dismiss();
break;
case R.id.tencent_btn:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
//將功能Scheme以URI的方式傳入data
Uri uri = Uri.parse("qqmap://map/routeplan?type=drive&to=我的目的地&tocoord=" + mInfo.getLat() + "," + mInfo.getLng());
intent.setData(uri);
if (intent.resolveActivity(getPackageManager()) != null) {
//啟動(dòng)該頁(yè)面即可
startActivity(intent);
} else {
Toast.makeText(this, "您尚未安裝騰訊地圖", Toast.LENGTH_LONG).show();
}
mBottomSheetPop.dismiss();
break;
}
}
效果圖
貼一下效果圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 百度地圖marker中圖片不顯示的解決方法(推薦)
- Android百度地圖添加Marker失真問(wèn)題的解決方案
- 關(guān)于Android高德地圖的簡(jiǎn)單開(kāi)發(fā)實(shí)例代碼(DEMO)
- Android中GPS坐標(biāo)轉(zhuǎn)換為高德地圖坐標(biāo)詳解
- Android開(kāi)發(fā)之高德地圖實(shí)現(xiàn)定位
- Android之高德地圖定位SDK集成及地圖功能實(shí)現(xiàn)
- Android 高德地圖之poi搜索功能的實(shí)現(xiàn)代碼
- GMap.Net開(kāi)發(fā)之自定義Marker使用方法
- Android基于高德地圖完全自定義Marker的實(shí)現(xiàn)方法
相關(guān)文章
Flutter實(shí)現(xiàn)旋轉(zhuǎn)掃描效果
這篇文章主要介紹了通過(guò)Flutter RotationTransition實(shí)現(xiàn)雷達(dá)旋轉(zhuǎn)掃描的效果,文中的示例代碼講解詳細(xì),感興趣的同學(xué)可以動(dòng)手試一試2022-01-01
Android中Rxjava實(shí)現(xiàn)三級(jí)緩存的兩種方式
這篇文章主要介紹了Android中Rxjava實(shí)現(xiàn)三級(jí)緩存的兩種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
Android 基于MediatorLiveData實(shí)現(xiàn)紅點(diǎn)的統(tǒng)一管理
這篇文章主要介紹了Android 基于MediatorLiveData實(shí)現(xiàn)紅點(diǎn)的統(tǒng)一管理,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04
Android 判斷當(dāng)前網(wǎng)絡(luò)是否可用簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android 判斷當(dāng)前網(wǎng)絡(luò)是否可用簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android調(diào)用系統(tǒng)默認(rèn)瀏覽器訪問(wèn)的方法
這篇文章主要介紹了Android調(diào)用系統(tǒng)默認(rèn)瀏覽器訪問(wèn)的方法的相關(guān)資料,需要的朋友可以參考下2016-03-03
android 仿ios數(shù)字密碼解鎖界面的實(shí)例
下面小編就為大家分享一篇android 仿ios數(shù)字密碼解鎖界面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Android實(shí)現(xiàn)TV端大圖瀏覽效果的全過(guò)程
最近的開(kāi)發(fā)中遇到了個(gè)需求,需要在tv端加載很長(zhǎng)的圖片,發(fā)現(xiàn)網(wǎng)上沒(méi)有相關(guān)的資料,所以跟大家分享下,這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)TV端大圖瀏覽效果的相關(guān)資料,需要的朋友可以參考下2023-01-01
Android中創(chuàng)建多線程管理器實(shí)例
這篇文章主要介紹了Android中創(chuàng)建多線程管理器實(shí)例,著重講解需要做的哪些事情,每一步都配有代碼例子,需要的朋友可以參考下2014-06-06

