public class MapUtil {
? ? public static final String PN_GAODE_MAP = "com.autonavi.minimap";// 高德地圖包名
? ? public static final String PN_BAIDU_MAP = "com.baidu.BaiduMap"; // 百度地圖包名
? ? public static final String PN_TENCENT_MAP = "com.tencent.map"; // 騰訊地圖包名
? ? /**
? ? * 檢查地圖應(yīng)用是否安裝
? ? * @return
? ? */
? ? public static boolean isGdMapInstalled(){
? ? ? ? return isInstallPackage(PN_GAODE_MAP);
? ? }
? ? public static boolean isBaiduMapInstalled(){
? ? ? ? return isInstallPackage(PN_BAIDU_MAP);
? ? }
? ? public static boolean isTencentMapInstalled(){
? ? ? ? return isInstallPackage(PN_TENCENT_MAP);
? ? }
? ? private static boolean isInstallPackage(String packageName) {
? ? ? ? return new File("/data/data/" + packageName).exists();
? ? }
? ? /**
? ? * 百度轉(zhuǎn)高德
? ? * @param bd_lat
? ? * @param bd_lon
? ? * @return
? ? */
? ? public static double[] bdToGaoDe(double bd_lat, double bd_lon) {
? ? ? ? double[] gd_lat_lon = new double[2];
? ? ? ? double PI = 3.14159265358979324 * 3000.0 / 180.0;
? ? ? ? double x = bd_lon - 0.0065, y = bd_lat - 0.006;
? ? ? ? double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI);
? ? ? ? double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI);
? ? ? ? gd_lat_lon[0] = z * Math.cos(theta);
? ? ? ? gd_lat_lon[1] = z * Math.sin(theta);
? ? ? ? return gd_lat_lon;
? ? }
? ? /**
? ? * 高德告抄、騰訊轉(zhuǎn)百度
? ? * @param gd_lon
? ? * @param gd_lat
? ? * @return
? ? */
? ? private static double[] gaoDeToBaidu(double gd_lon, double gd_lat) {
? ? ? ? double[] bd_lat_lon = new double[2];
? ? ? ? double PI = 3.14159265358979324 * 3000.0 / 180.0;
? ? ? ? double x = gd_lon, y = gd_lat;
? ? ? ? double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * PI);
? ? ? ? double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * PI);
? ? ? ? bd_lat_lon[0] = z * Math.cos(theta) + 0.0065;
? ? ? ? bd_lat_lon[1] = z * Math.sin(theta) + 0.006;
? ? ? ? return bd_lat_lon;
? ? }
? ? /**
? ? * 百度坐標(biāo)系 (BD-09) 與 火星坐標(biāo)系 (GCJ-02)的轉(zhuǎn)換
? ? * 即 百度 轉(zhuǎn) 谷歌撰茎、高德
? ? *
? ? * @param latLng
? ? * @returns
? ? *
? ? * 使用此方法需要下載導(dǎo)入百度地圖的BaiduLBS_Android.jar包
? ? */
? ? public static LatLng BD09ToGCJ02(LatLng latLng) {
? ? ? ? double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
? ? ? ? double x = latLng.longitude - 0.0065;
? ? ? ? double y = latLng.latitude - 0.006;
? ? ? ? double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
? ? ? ? double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
? ? ? ? double gg_lat = z * Math.sin(theta);
? ? ? ? double gg_lng = z * Math.cos(theta);
? ? ? ? return new LatLng(gg_lat, gg_lng);
? ? }
? ? /**
? ? * 火星坐標(biāo)系 (GCJ-02) 與百度坐標(biāo)系 (BD-09) 的轉(zhuǎn)換
? ? * 即谷歌、高德 轉(zhuǎn) 百度
? ? *
? ? * @param latLng
? ? * @returns
? ? *
? ? * 需要百度地圖的BaiduLBS_Android.jar包
? ? */
? ? public static LatLng GCJ02ToBD09(LatLng latLng) {
? ? ? ? double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
? ? ? ? double z = Math.sqrt(latLng.longitude * latLng.longitude + latLng.latitude * latLng.latitude) + 0.00002 * Math.sin(latLng.latitude * x_pi);
? ? ? ? double theta = Math.atan2(latLng.latitude, latLng.longitude) + 0.000003 * Math.cos(latLng.longitude * x_pi);
? ? ? ? double bd_lat = z * Math.sin(theta) + 0.006;
? ? ? ? double bd_lng = z * Math.cos(theta) + 0.0065;
? ? ? ? return new LatLng(bd_lat, bd_lng);
? ? }
? ? /**
? ? * 打開高德地圖導(dǎo)航功能
? ? * @param context
? ? * @param slat 起點(diǎn)緯度
? ? * @param slon 起點(diǎn)經(jīng)度
? ? * @param sname 起點(diǎn)名稱 可不填(0,0打洼,null)
? ? * @param dlat 終點(diǎn)緯度
? ? * @param dlon 終點(diǎn)經(jīng)度
? ? * @param dname 終點(diǎn)名稱 必填
? ? */
? ? public static void openGaoDeNavi(Context context,double slat, double slon, String sname, double dlat, double dlon, String dname){
? ? ? ? String uriString = null;
? ? ? ? StringBuilder builder = new StringBuilder("amapuri://route/plan?sourceApplication=maxuslife");
? ? ? ? if (slat != 0) {
? ? ? ? ? ? builder.append("&sname=").append(sname)
? ? ? ? ? ? ? ? ? ? .append("&slat=").append(slat)
? ? ? ? ? ? ? ? ? ? .append("&slon=").append(slon);
? ? ? ? }
? ? ? ? builder.append("&dlat=").append(dlat)
? ? ? ? ? ? ? ? .append("&dlon=").append(dlon)
? ? ? ? ? ? ? ? .append("&dname=").append(dname)
? ? ? ? ? ? ? ? .append("&dev=0")
? ? ? ? ? ? ? ? .append("&t=0");
? ? ? ? uriString = builder.toString();
? ? ? ? Intent intent = new Intent(Intent.ACTION_VIEW);
? ? ? ? intent.setPackage(PN_GAODE_MAP);
? ? ? ? intent.setData(Uri.parse(uriString));
? ? ? ? context.startActivity(intent);
? ? }
? ? /**
? ? * 打開騰訊地圖
? ? * params 參考http://lbs.qq.com/uri_v1/guide-route.html
? ? *
? ? * @param context
? ? * @param slat 起點(diǎn)緯度
? ? * @param slon 起點(diǎn)經(jīng)度
? ? * @param sname 起點(diǎn)名稱 可不填(0,0龄糊,null)
? ? * @param dlat 終點(diǎn)緯度
? ? * @param dlon 終點(diǎn)經(jīng)度
? ? * @param dname 終點(diǎn)名稱 必填
? ? * 駕車:type=drive逆粹,policy有以下取值
? ? 0:較快捷
? ? 1:無高速
? ? 2:距離
? ? policy的取值缺省為0
? ? * &from=" + dqAddress + "&fromcoord=" + dqLatitude + "," + dqLongitude + "
? ? */
? ? public static void openTencentMap(Context context, double slat, double slon, String sname, double dlat, double dlon, String dname) {
? ? ? ? String uriString = null;
? ? ? ? StringBuilder builder = new StringBuilder("qqmap://map/routeplan?type=drive&policy=0&referer=zhongshuo");
? ? ? ? if (slat != 0) {
? ? ? ? ? ? builder.append("&from=").append(sname)
? ? ? ? ? ? ? ? ? ? .append("&fromcoord=").append(slat)
? ? ? ? ? ? ? ? ? ? .append(",")
? ? ? ? ? ? ? ? ? ? .append(slon);
? ? ? ? }
? ? ? ? builder.append("&to=").append(dname)
? ? ? ? ? ? ? ? .append("&tocoord=").append(dlat)
? ? ? ? ? ? ? ? .append(",")
? ? ? ? ? ? ? ? .append(dlon);
? ? ? ? uriString = builder.toString();
? ? ? ? Intent intent = new Intent(Intent.ACTION_VIEW);
? ? ? ? intent.setPackage(PN_TENCENT_MAP);
? ? ? ? intent.setData(Uri.parse(uriString));
? ? ? ? context.startActivity(intent);
? ? }
? ? /**
? ? * 打開百度地圖導(dǎo)航功能(默認(rèn)坐標(biāo)點(diǎn)是高德地圖,需要轉(zhuǎn)換)
? ? * @param context
? ? * @param slat 起點(diǎn)緯度
? ? * @param slon 起點(diǎn)經(jīng)度
? ? * @param sname 起點(diǎn)名稱 可不填(0,0炫惩,null)
? ? * @param dlat 終點(diǎn)緯度
? ? * @param dlon 終點(diǎn)經(jīng)度
? ? * @param dname 終點(diǎn)名稱 必填
? ? */
? ? public static void openBaiDuNavi(Context context,double slat, double slon, String sname, double dlat, double dlon, String dname){
? ? ? ? String uriString = null;
? ? ? ? //終點(diǎn)坐標(biāo)轉(zhuǎn)換
//? ? ? ? 此方法需要百度地圖的BaiduLBS_Android.jar包
//? ? ? ? LatLng destination = new LatLng(dlat,dlon);
//? ? ? ? LatLng destinationLatLng = GCJ02ToBD09(destination);
//? ? ? ? dlat = destinationLatLng.latitude;
//? ? ? ? dlon = destinationLatLng.longitude;
? ? ? ? double destination[] = gaoDeToBaidu(dlat, dlon);
? ? ? ? dlat = destination[0];
? ? ? ? dlon = destination[1];
? ? ? ? StringBuilder builder = new StringBuilder("baidumap://map/direction?mode=driving&");
? ? ? ? if (slat != 0){
? ? ? ? ? ? //起點(diǎn)坐標(biāo)轉(zhuǎn)換
//? ? ? ? ? ? LatLng origin = new LatLng(slat,slon);
//? ? ? ? ? ? LatLng originLatLng = GCJ02ToBD09(origin);
//? ? ? ? ? ? slat = originLatLng.latitude;
//? ? ? ? ? ? slon = originLatLng.longitude;
? ? ? ? ? ? double[] origin = gaoDeToBaidu(slat, slon);
? ? ? ? ? ? slat = origin[0];
? ? ? ? ? ? slon = origin[1];
? ? ? ? ? ? builder.append("origin=latlng:")
? ? ? ? ? ? ? ? ? ? .append(slat)
? ? ? ? ? ? ? ? ? ? .append(",")
? ? ? ? ? ? ? ? ? ? .append(slon)
? ? ? ? ? ? ? ? ? ? .append("|name:")
? ? ? ? ? ? ? ? ? ? .append(sname);
? ? ? ? }
? ? ? ? builder.append("&destination=latlng:")
? ? ? ? ? ? ? ? .append(dlat)
? ? ? ? ? ? ? ? .append(",")
? ? ? ? ? ? ? ? .append(dlon)
? ? ? ? ? ? ? ? .append("|name:")
? ? ? ? ? ? ? ? .append(dname);
? ? ? ? uriString = builder.toString();
? ? ? ? Intent intent = new Intent(Intent.ACTION_VIEW);
? ? ? ? intent.setPackage(PN_BAIDU_MAP);
? ? ? ? intent.setData(Uri.parse(uriString));
? ? ? ? context.startActivity(intent);
? ? }
}
/**
? ? * 打開網(wǎng)頁版 導(dǎo)航
? ? * @param context
? ? * @param myLatLng 起點(diǎn)經(jīng)緯度
? ? * @param myAddress 起點(diǎn)地址名展示
? ? * @param destination 終點(diǎn)經(jīng)緯度
? ? * @param destinationAddress 終點(diǎn)地址名展示
? ? */
? ? public static void openBrowserMap(Context context, LatLng myLatLng, String myAddress, LatLng destination,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String destinationAddress) {
? ? ? ? Intent intent = new Intent();
? ? ? ? intent.setAction("android.intent.action.VIEW");
? ? ? ? intent.setData(Uri.parse("http://uri.amap.com/navigation?" +
? ? ? ? ? ? "from=" + myLatLng.longitude + "," + myLatLng.latitude + "," + myAddress +
? ? ? ? ? ? "to=" + destination.longitude + "," + destination.latitude + "," + destinationAddress +
? ? ? ? ? ? "&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0"));
? ? ? ? context.startActivity(intent);
? ? }