Android調(diào)用第三方(高德懒闷、百度、騰訊)地圖以及瀏覽器打開網(wǎng)頁地圖

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

? ? }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末僻弹,一起剝皮案震驚了整個濱河市,隨后出現(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ī)與錄音姜胖,去河邊找鬼誉帅。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蚜锨。 我是一名探鬼主播档插,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼亚再!你這毒婦竟也來了郭膛?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,671評論 0 276
  • 序言:老撾萬榮一對情侶失蹤氛悬,失蹤者是張志新(化名)和其女友劉穎饲鄙,沒想到半個月后,有當(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
  • 正文 我和宋清朗相戀三年帆谍,在試婚紗的時候發(fā)現(xiàn)自己被綠了伪朽。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,444評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡汛蝙,死狀恐怖烈涮,靈堂內(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. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 48,837評論 3 376
  • 正文 我出身青樓室叉,卻偏偏與公主長得像睹栖,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子茧痕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,455評論 2 359

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