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;
}
}