Android 調(diào)用高德+百度地圖導(dǎo)航

import android.content.Context;

import android.content.Intent;

import android.net.Uri;

import com.jiarui.base.bases.BaseApplication;

import com.jiarui.base.utils.StringUtils;

import com.jiarui.base.utils.ToastUitl;

import com.tuba.android.tuba40.R;

import com.tuba.android.tuba40.dialog.CommonDialog;

import java.io.File;

import static java.lang.Math.atan2;

import static java.lang.Math.cos;

import static java.lang.Math.sin;

import static java.lang.Math.sqrt;

/**

* ======================================================>

*

* @Todo: 默認(rèn)使用的是高德經(jīng)緯度坐標(biāo), 不同坐標(biāo)系之間需要轉(zhuǎn)換

* 如果高德地圖進(jìn)入會提示"定位失敗,請重試"奈梳,可以改成指定起點(diǎn)和終點(diǎn)的坐標(biāo)

* @Author:Lion-082

* @Date:2018/10/18

* @Time:下午 02:37

* ======================================================>

*/

public class NavigationUtil {

? ? /**

? ? * 顯示選擇導(dǎo)航方式dialog

*

? ? * @paramcontext

? ? * @paramendLongitude

? ? * @paramendLatitude

? ? * @paramendAddress

? ? */

? ? public static void showNavDialog(final Context context, final String endLongitude, final String endLatitude, final String endAddress) {

? ? ? ? final String[] titles= {"百度地圖", "高德地圖"};

? ? ? ? final CommonDialog dialog= new CommonDialog(context, titles);

? ? ? ? dialog.setOnClickListener(new CommonDialog.OnClickListener() {

? ? ? ? ? ? @Override

? ? ? ? ? ? public void onItemClick(final int position) {

? ? ? ? ? ? ? ? //調(diào)用地圖導(dǎo)航

? ? ? ? ? ? ? ? if (StringUtils.isNotEmpty(endLatitude) && StringUtils.isNotEmpty(endLongitude) && StringUtils.isNotEmpty(endAddress)) {

//? ? ? ? ? ? ? ? ? ? (1)

//? ? ? ? ? ? ? ? ? ? if (0 == position) {

//? ? ? ? ? ? ? ? ? ? ? ? //調(diào)用百度導(dǎo)航 - 高德坐標(biāo)

//? ? ? ? ? ? ? ? ? ? ? ? NavigationUtil.initBaiduNavigation_GaodeCoordinate(context, endLongitude, endLatitude, endAddress);

//? ? ? ? ? ? ? ? ? ? } else {

//? ? ? ? ? ? ? ? ? ? ? ? //調(diào)用高德導(dǎo)航 - 高德坐標(biāo)

//? ? ? ? ? ? ? ? ? ? ? ? NavigationUtil.initAMapNavigation_GaodeCoordinate(context, endLongitude, endLatitude, endAddress);

//? ? ? ? ? ? ? ? ? ? }

//? ? ? ? ? ? ? ? ? ? (2)

? ? ? ? ? ? ? ? ? ? if (0 == position) {

? ? ? ? ? ? ? ? ? ? ? ? //調(diào)用百度導(dǎo)航 - 百度坐標(biāo)

? ? ? ? ? ? ? ? ? ? ? ? NavigationUtil.initBaiduNavigation_BaiduCoordinate(context, endLongitude, endLatitude, endAddress);

? ? ? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ? ? //調(diào)用高德導(dǎo)航 - 百度坐標(biāo)

? ? ? ? ? ? ? ? ? ? ? ? NavigationUtil.initAMapNavigation_BaiduCoordinate(context, endLongitude, endLatitude, endAddress);

}

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ToastUitl.showShort(context,"終點(diǎn)信息獲取失敗");

}

? ? ? ? ? ? ? ? dialog.dismiss();

}

});

? ? ? ? dialog.show();

}

? ? ///////////////////////////////////////////////////////////////////////////

? ? // 高德坐標(biāo)

? ? ///////////////////////////////////////////////////////////////////////////

/**

? ? * 調(diào)用百度導(dǎo)航 - 高德坐標(biāo)

? ? *

? ? * @paramcontext? 上下文對象

? ? * @parameLon? ? 終點(diǎn)經(jīng)度

? ? * @parameLat? ? 終點(diǎn)緯度

? ? * @parameAddress 終點(diǎn)地址

? ? */

? ? private static void initBaiduNavigation_GaodeCoordinate(final Context context, final String eLon, final String eLat, final String eAddress) {

? ? ? ? //是否安裝百度地圖

? ? ? ? if (isInstallBaiduMap(context)) {

? ? ? ? ? ? /*高德坐標(biāo)轉(zhuǎn)百度坐標(biāo)*/

? ? ? ? ? ? double[] endLatLng= gcjToBd(Double.parseDouble(eLon), Double.parseDouble(eLat));

? ? ? ? ? ? openBaiduMap(context, endLatLng[0], endLatLng[1], eAddress);

}

}

? ? /**

? ? * 調(diào)用高德導(dǎo)航 - 高德坐標(biāo)

? ? *

? ? * @paramcontext? 上下文對象

? ? * @parameLon? ? 終點(diǎn)經(jīng)度

? ? * @parameLat? ? 終點(diǎn)緯度

? ? * @parameAddress 終點(diǎn)地址

? ? */

? ? private static void initAMapNavigation_GaodeCoordinate(final Context context, final String eLon, final String eLat, final String eAddress) {

? ? ? ? //是否安裝高德地圖

? ? ? ? if (isInstallAMap(context)) {

? ? ? ? ? ? openAMap(context, Double.parseDouble(eLon), Double.parseDouble(eLat), eAddress);

}

}

? ? ///////////////////////////////////////////////////////////////////////////

? ? // 百度坐標(biāo)

? ? ///////////////////////////////////////////////////////////////////////////

/**

? ? * 調(diào)用百度導(dǎo)航 - 百度坐標(biāo)

? ? *

? ? * @paramcontext? 上下文對象

? ? * @parameLon? ? 終點(diǎn)經(jīng)度

? ? * @parameLat? ? 終點(diǎn)緯度

? ? * @parameAddress 終點(diǎn)地址

? ? */

? ? private static void initBaiduNavigation_BaiduCoordinate(final Context context, final String eLon, final String eLat, final String eAddress) {

? ? ? ? //是否安裝百度地圖

? ? ? ? if (isInstallBaiduMap(context)) {

? ? ? ? ? ? openBaiduMap(context, Double.parseDouble(eLon), Double.parseDouble(eLat), eAddress);

}

}

? ? /**

? ? * 調(diào)用高德導(dǎo)航 - 百度坐標(biāo)

? ? *

? ? * @paramcontext? 上下文對象

? ? * @parameLon? ? 終點(diǎn)經(jīng)度

? ? * @parameLat? ? 終點(diǎn)緯度

? ? * @parameAddress 終點(diǎn)地址

? ? */

? ? private static void initAMapNavigation_BaiduCoordinate(final Context context, final String eLon, final String eLat, final String eAddress) {

? ? ? ? //是否安裝高德地圖

? ? ? ? if (isInstallAMap(context)) {

? ? ? ? ? ? /*百度坐標(biāo)轉(zhuǎn)高德坐標(biāo)+bd_decrypt+bdToGcj*/

? ? ? ? ? ? double[] endLatLng= bdToGcj(Double.parseDouble(eLon), Double.parseDouble(eLat));

? ? ? ? ? ? openAMap(context, endLatLng[0], endLatLng[1], eAddress);

}

}

? ? /*------------------------------------(分割線)--調(diào)用第三方導(dǎo)航--2018/10/18 下午 01:56------------------------------------*/

? ? /**

? ? * 打開百度地圖

? ? *

? ? * @paramcontext? ? ? 上下文對象

? ? * @paramendLon? ? ? 終點(diǎn)經(jīng)度

? ? * @paramendLat? ? ? 終點(diǎn)緯度

? ? * @paramendDescrible 終點(diǎn)地址

? ? */

? ? private static void openBaiduMap(final Context context, double endLon, double endLat, String endDescrible) {

? ? ? ? try {

? ? ? ? ? ? /*調(diào)用導(dǎo)航*/

? ? ? ? ? ? StringBuffer loc= new StringBuffer("baidumap://map/navi?");

? ? ? ? ? ? loc.append("&src=").append(R.string.app_name + "|" + R.string.app_name);

? ? ? ? ? ? loc.append("&location=").append(endLat+ "," + endLon);

? ? ? ? ? ? Intent intent= Intent.getIntent(loc.toString());

? ? ? ? ? ? context.startActivity(intent); //啟動(dòng)調(diào)用

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

}

}

? ? /**

? ? * 打開高德地圖

? ? *

? ? * @paramcontext? ? ? 上下文對象

? ? * @paramendLon? ? ? 終點(diǎn)經(jīng)度

? ? * @paramendLat? ? ? 終點(diǎn)緯度

? ? * @paramendDescrible 終點(diǎn)地址

? ? */

? ? private static void openAMap(final Context context, double endLon, double endLat, String endDescrible) {

? ? ? ? try {

? ? ? ? ? ? /*調(diào)用導(dǎo)航*/

? ? ? ? ? ? StringBuilder loc= new StringBuilder();

? ? ? ? ? ? loc.append("androidamap://navi?sourceApplication=");

? ? ? ? ? ? loc.append(R.string.app_name);

? ? ? ? ? ? loc.append("&lat=");

? ? ? ? ? ? loc.append(endLat);

? ? ? ? ? ? loc.append("&lon=");

? ? ? ? ? ? loc.append(endLon);

? ? ? ? ? ? loc.append("&dev=");//起終 點(diǎn)是否偏移(0:lat和lon是已經(jīng)加密后的,不需要國測加密: 1:需要國測加密)

? ? ? ? ? ? loc.append(0);//百度坐標(biāo)調(diào)用高德的時(shí)候設(shè)置為0聋迎,因?yàn)榘俣鹊貓D坐標(biāo)已經(jīng)加密了

//? ? ? ? ? ? loc.append("&dname=").append(endDescrible);

? ? ? ? ? ? loc.append("&style=");

? ? ? ? ? ? loc.append(2);

? ? ? ? ? ? Intent intent= new Intent("android.intent.action.VIEW", Uri.parse(loc.toString()));

? ? ? ? ? ? intent.setPackage("com.autonavi.minimap");

? ? ? ? ? ? context.startActivity(intent);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

}

}

? ? /**

? ? * 檢查是否安裝百度地圖

? ? */

? ? private static boolean isInstallBaiduMap(Context context) {

? ? ? ? // 判斷是否安裝百度地圖

? ? ? ? if (new File("/data/data/" + "com.baidu.BaiduMap").exists()) {

? ? ? ? ? ? return true;

? ? ? ? } else {

? ? ? ? ? ? // 未安裝

? ? ? ? ? ? // 顯示手機(jī)上所有的market商店

? ? ? ? ? ? try {

? ? ? ? ? ? ? ? ToastUitl.showShort(BaseApplication.getAppContext(),"未安裝百度地圖");

? ? ? ? ? ? ? ? Uri uri= Uri.parse("market://details?id=com.baidu.BaiduMap");

? ? ? ? ? ? ? ? Intent intent= new Intent(Intent.ACTION_VIEW, uri);

? ? ? ? ? ? ? ? context.startActivity(intent);

? ? ? ? ? ? } catch (Exception e) {

? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? ? ? ToastUitl.showShort(BaseApplication.getAppContext(),"請先安裝百度地圖");

? ? ? ? ? ? ? ? return false;

}

? ? ? ? ? ? return false;

}

}

? ? /**

? ? * 檢查是否安裝高德地圖

? ? */

? ? private static boolean isInstallAMap(Context context) {

? ? ? ? // 判斷是否安裝高德地圖

? ? ? ? if (new File("/data/data/" + "com.autonavi.minimap").exists()) {

? ? ? ? ? ? return true;

? ? ? ? } else {

? ? ? ? ? ? try {

? ? ? ? ? ? ? ? ToastUitl.showShort(BaseApplication.getAppContext(),"未安裝高德地圖");//未安裝

? ? ? ? ? ? ? ? Uri uri= Uri.parse("market://details?id=com.autonavi.minimap");

? ? ? ? ? ? ? ? Intent intent= new Intent(Intent.ACTION_VIEW, uri);

? ? ? ? ? ? ? ? context.startActivity(intent);

? ? ? ? ? ? } catch (Exception e) {

? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? ? ? ToastUitl.showShort(BaseApplication.getAppContext(),"請先安裝高德地圖");

? ? ? ? ? ? ? ? return false;

}

}

? ? ? ? return false;

}

? ? //百度轉(zhuǎn)高德-直接用Math.PI

? ? public static double[]? bd_decrypt(double bd_lon, double bd_lat) {

? ? ? ? double[] gd_lat_lon= new double[2];

? ? ? ? double x= bd_lon- 0.0065, y= bd_lat- 0.006;

? ? ? ? double z= sqrt(x* x+ y* y) - 0.00002 * sin(y* Math.PI);

? ? ? ? double theta= atan2(y, x) - 0.000003 * cos(x* Math.PI);

? ? ? ? gd_lat_lon[0] = z* cos(theta);

? ? ? ? gd_lat_lon[1] = z* sin(theta);

? ? ? ? return gd_lat_lon;

}

? ? /**

? ? * 百度 => 高德

? ? * BD-09 => GCJ-02

*

? ? * @parambdLon 經(jīng)度

? ? * @parambdLat 緯度

? ? */

? ? public static double[] bdToGcj(double bdLon, double bdLat) {

? ? ? ? double[] gd_lat_lon= new double[2];

? ? ? ? double PI= 3.14159265358979324 * 3000.0 / 180.0;

? ? ? ? double x= bdLon- 0.0065, y= bdLat- 0.006;

? ? ? ? double z= sqrt(x* x+ y* y) - 0.00002 * sin(y* PI);

? ? ? ? double theta= atan2(y, x) - 0.000003 * cos(x* PI);

? ? ? ? gd_lat_lon[0] = z* cos(theta);

? ? ? ? gd_lat_lon[1] = z* sin(theta);

? ? ? ? return gd_lat_lon;

}

? ? /**

? ? * 高德 => 百度

? ? *

? ? * @paramgdLon 經(jīng)度

? ? * @paramgdLat 緯度

? ? */

? ? public static double[] gcjToBd(double gdLon, double gdLat) {

? ? ? ? double[] bd_lat_lon= new double[2];

? ? ? ? double PI= 3.14159265358979324 * 3000.0 / 180.0;

? ? ? ? double x= gdLon, y= gdLat;

? ? ? ? double z= sqrt(x* x+ y* y) + 0.00002 * sin(y* PI);

? ? ? ? double theta= atan2(y, x) + 0.000003 * cos(x* PI);

? ? ? ? bd_lat_lon[0] = z* cos(theta) + 0.0065;

? ? ? ? bd_lat_lon[1] = z* sin(theta) + 0.006;

? ? ? ? return bd_lat_lon;

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末痕鳍,一起剝皮案震驚了整個(gè)濱河市纷捞,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌顿膨,老刑警劉巖萤皂,帶你破解...
    沈念sama閱讀 221,430評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異竭鞍,居然都是意外死亡板惑,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評論 3 398
  • 文/潘曉璐 我一進(jìn)店門偎快,熙熙樓的掌柜王于貴愁眉苦臉地迎上來冯乘,“玉大人,你說我怎么就攤上這事晒夹●陕” “怎么了姊氓?”我有些...
    開封第一講書人閱讀 167,834評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長喷好。 經(jīng)常有香客問我翔横,道長,這世上最難降的妖魔是什么梗搅? 我笑而不...
    開封第一講書人閱讀 59,543評論 1 296
  • 正文 為了忘掉前任禾唁,我火速辦了婚禮,結(jié)果婚禮上无切,老公的妹妹穿的比我還像新娘荡短。我一直安慰自己,他們只是感情好订雾,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,547評論 6 397
  • 文/花漫 我一把揭開白布肢预。 她就那樣靜靜地躺著,像睡著了一般洼哎。 火紅的嫁衣襯著肌膚如雪烫映。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,196評論 1 308
  • 那天噩峦,我揣著相機(jī)與錄音锭沟,去河邊找鬼。 笑死识补,一個(gè)胖子當(dāng)著我的面吹牛族淮,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播凭涂,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼祝辣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了切油?” 一聲冷哼從身側(cè)響起蝙斜,我...
    開封第一講書人閱讀 39,671評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎澎胡,沒想到半個(gè)月后孕荠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,221評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡攻谁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,303評論 3 340
  • 正文 我和宋清朗相戀三年稚伍,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片戚宦。...
    茶點(diǎn)故事閱讀 40,444評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡个曙,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出受楼,到底是詐尸還是另有隱情困檩,我是刑警寧澤祠挫,帶...
    沈念sama閱讀 36,134評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站悼沿,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏骚灸。R本人自食惡果不足惜糟趾,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,810評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望甚牲。 院中可真熱鬧义郑,春花似錦、人聲如沸丈钙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽雏赦。三九已至劫笙,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間星岗,已是汗流浹背填大。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留俏橘,地道東北人允华。 一個(gè)月前我還...
    沈念sama閱讀 48,837評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像寥掐,于是被迫代替她去往敵國和親靴寂。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,455評論 2 359

推薦閱讀更多精彩內(nèi)容