最近在項目中需要地圖導(dǎo)航功能,但是導(dǎo)入第三方地圖導(dǎo)航包的話涯竟,應(yīng)用打出包后會很大赡鲜,所以考慮到可以通過調(diào)用外部地圖應(yīng)用來解決空厌,參考了微信打開外部地圖的做法,有了下面的解決辦法:
原理
- 通過高德银酬、百度嘲更、騰訊提供的Uri打開應(yīng)用外部地圖app.
高德、百度揩瞪、騰訊官方uri調(diào)起app文檔傳送門##
- 高德地圖:http://lbs.amap.com/api/amap-mobile/guide/android/route
- 百度地圖:http://lbsyun.baidu.com/index.php?title=uri/api/android
- 騰訊地圖:http://lbs.qq.com/uri_v1/index.html
關(guān)鍵代碼
-以高德地圖為例:
/**
* 打開高德
*
* @param latitude
* @param longitude
*/
private static void openGaode(Activity activity, String title, double latitude, double longitude) {
if (AppUtils.isAvilible("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://route/plan/?dlat=" + latitude + "&dlon=" + longitude + "&dname=" + title + "&dev=0&t=0");
intent.setData(uri);
//啟動該頁面即可
activity.startActivity(intent);
} else {
ToastUtils.showToast("您尚未安裝高德地圖");
Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (intent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivity(intent);
}
}
}
需要注意的地方
-1.由于自己應(yīng)用中集成的是高德地圖赋朦,用的是GCJ-02坐標(biāo)系(火星坐標(biāo)系),所以當(dāng)打開百度地圖時需要進(jìn)行坐標(biāo)轉(zhuǎn)換李破,將火星坐標(biāo)系轉(zhuǎn)換成百度坐標(biāo)系BD09宠哄。
具體看代碼: CoodinateCovertor.java
使用方法
-1.Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
-2.Add the dependency
dependencies {
compile 'com.github.lintianlin:OpenMapDemo:v1.0.0'
}
示例效果
pic4.jpg
pic1.jpg
pic2.jpg
pic3.jpg