android開啟外部地圖導航(百度、高德、騰訊)
阿新 • • 發佈:2018-11-15
1.參考下面的地址:我的呼叫百度的就是採用這個文章的方法
https://blog.csdn.net/hyyz3293/article/details/76836633
2,我自己採用的方法,如下;
//todo:獲取當前自己的位置; getLocation(new BDLocationListener() { @Override public void onReceiveLocation(BDLocation location) { MLog.d(TAG, location.toString()); dismissDialog(); floatLatitudeNow = (float) location.getLatitude(); float LongitudeNow = (float) location.getLongitude(); if (LatitudeNow > 0 && LongitudeNow > 0) { SharedPreferencesTool.setEditor(context, "LatitudeNow", LatitudeNow); SharedPreferencesTool.setEditor(context, "LongitudeNow", LongitudeNow); } MLog.i("LatitudeNow",LatitudeNow+""); MLog.i("LongitudeNow",LongitudeNow+""); } });
mMap = new ArrayList<>(); if (isAppInstalled("com.google.android.apps.maps")) { mMap.add("谷歌地圖"); } if (isAppInstalled("com.baidu.BaiduMap")) { mMap.add("百度地圖"); } if (isAppInstalled("com.autonavi.minimap")) { mMap.add("高德地圖"); } if (isAppInstalled("com.tencent.map")) { mMap.add("騰訊地圖"); } if (mMap.size() != 0) { final String[] maps = new String[mMap.size()]; for (String s : mMap) { Log.e("1111", s); } for (int i = 0; i < mMap.size(); i++) { maps[i] = mMap.get(i); }
AlertDialog.Builder builder = new AlertDialog.Builder(ExtWebDetailActivity.this); builder.setTitle("請選擇"); //設定一個下拉的列表選擇項 builder.setItems(maps, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { float latitudeNow = SharedPreferencesTool.getSharedPreferences(ExtWebDetailActivity.this, "LatitudeNow", -1f); float LongitudeNow = SharedPreferencesTool.getSharedPreferences(ExtWebDetailActivity.this, "LongitudeNow", -1f); double[] doubles1 = GPSUtil.bd09_To_Gcj02(latitudeNow, LongitudeNow); double latitudeNow1 = doubles1[0]; double LongitudeNow1 = doubles1[1]; double[] doubles2 = GPSUtil.bd09_To_Gcj02(Float.valueOf(weidu) , Float.valueOf(jingdu)); double latitudeNow2 = doubles2[0]; double LongitudeNow2 = doubles2[1]; if (maps[which].equals("騰訊地圖")) { // 騰訊地圖 // Log.e("1111", "開啟騰訊地圖"); Intent naviIntent = new Intent("android.intent.action.VIEW", android.net.Uri.parse("qqmap://map/routeplan?type=drive&from="+"我的位置"+"&fromcoord="+latitudeNow1+","+LongitudeNow1+"&to=" +"目的地"+ "&tocoord=" + latitudeNow2 + "," + LongitudeNow2+ "&policy=0&referer="+"翠微e生活")); ExtWebDetailActivity.this.startActivity(naviIntent); } else if (maps[which].equals("百度地圖")) { try { //這個方法是沒用的 // Intent intent = Intent.getIntent("intent://map/direction?" + // "origin=latlng:"+latitudeNow+","+LongitudeNow+ // "&destination=latlng:"+Float.valueOf(weidu)+","+Float.valueOf(jingdu)+"|name:我的目的地"+ //終點 // "&mode=driving&" + //導航路線方式 // "region=北京" + // // "&src=翠微e生活#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end"); // openBaiduMap(latitudeNow,LongitudeNow,"我的位置",Float.valueOf(weidu),Float.valueOf(jingdu),"目的地",""); } catch (Exception e) { Log.e("intent", e.getMessage()); } } else if (maps[which].equals("谷歌地圖")) { Uri gmmIntentUri = Uri.parse("google.navigation:q=" + jingdu + "," + weidu + ", + BeiJing +China"); Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri); mapIntent.setPackage("com.google.android.apps.maps"); ExtWebDetailActivity.this.startActivity(mapIntent); } else if (maps[which].equals("高德地圖")) { Log.e("1111", "開啟高德地圖"); try { MLog.i("LatitudeNow",latitudeNow+""); MLog.i("LongitudeNow",LongitudeNow+""); Intent intent = Intent.getIntent("androidamap://route?sourceApplication="+"翠微e生活"+"&slat="+latitudeNow1+"&slon="+LongitudeNow1+"&sname="+"我的位置"+"&dlat="+latitudeNow2+"&dlon="+LongitudeNow2+"&dname="+"目的地"+"&dev=0&m=0&t=1"); ExtWebDetailActivity.this.startActivity(intent); } catch (URISyntaxException e) { e.printStackTrace(); } } } }); builder.show(); } else { Log.e("1111", "沒有地圖"); Toast.makeText(ExtWebDetailActivity.this, "您尚未安裝地圖APP", Toast.LENGTH_SHORT).show(); } } });
這個要主要,在百度地圖地圖和谷歌,高德地圖的時候,需要轉換座標系,不然導航的位置就不是很準確了。
https://blog.csdn.net/a13570320979/article/details/51366355#