調用高德地圖、百度地圖客戶端

調用高德地圖、百度地圖客戶端(Android版)


今天項目中要調用百度地圖和高德地圖的導航功能,上網搜了很多資料,介紹的都很侷限,當然最好的學習方式是查看官方文檔了。

百度地圖URI:

http://lbsyun.baidu.com/index.php?title=uri/api/android

高德地圖URI:

http://lbs.amap.com/api/amap-mobile/guide/android/navigation

當時搜資料時高德地圖URI網上給的是過時,坑的我在開發者平臺上找了好久。。。。



ok,我們現在來看看效果圖和參數

百度地圖:

這裏寫圖片描述

高德地圖:

這裏寫圖片描述

言歸正傳,開始分享我的代碼了,我寫了一個工具類MapUtil,比較簡單,各位可以根據上面看到的參數自行增減

public class MapUtil {


    /**
     * 獲取打開百度地圖應用
     * @param context 上下文對象
     * @param originLat 起點經度
     * @param originLon 起點緯度
     * @param desLat 終點經度
     * @param desLon 終點緯度
     * @return
     *
     * mode:導航模式,可選transit(公交)、driving(駕車)、walking(步行)和riding(騎行).默認:driving
     *
     */
    public static void getBaiduMapUri(Context context,String originLat, String originLon,String desLat, String desLon){
      String uri="baidumap://map/direction?origin=name:我的位置|latlng:"+originLat+","+originLon+"&destination=name:目的地|latlng:"+desLat+","+desLon+"&mode=driving";
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
      intent.setPackage("com.baidu.BaiduMap");
      context.startActivity(intent);
    }


    /**
     * 啓動高德App進行導航
     *
     * @param slat 起點緯度。如果不填寫此參數則自動將用戶當前位置設爲起點緯度。
     * @param slon 起點經度。如果不填寫此參數則自動將用戶當前位置設爲起點經度。
     * @param dlat 終點緯度
     * @param dlon 終點經度
     *
     *  dev 必填 是否偏移(0:lat 和 lon 是已經加密後的,不需要國測加密; 1:需要國測加密)
     *  t 必填 t = 0(駕車)= 1(公交)= 2(步行)= 3(騎行)= 4(火車)= 5(長途客車)(騎行僅在V788以上版本支持)
     *
     */
    public static  void getGaoDeMapUri(Context context, String slat , String slon ,String dlat , String dlon){
       String uri="amapuri://route/plan/?slat="+slat+"&slon="+slon+"&sname=我的位置&dlat="+dlat+"&dlon="+dlon+"&dname=目的地&dev=0&t=0";
        Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse(uri));
        intent.setPackage("com.autonavi.minimap");
        context.startActivity(intent);
    }


    /**
     * 根據包名檢測某個APP是否安裝
     * @param packageName 包名
     * @return true 安裝 false 沒有安裝
     */
    public static boolean isInstallByRead(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }

    /**
     * 百度地圖定位經緯度轉高德經緯度
     * @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;
    }

    /**
     * 高德地圖定位經緯度轉百度經緯度
     * @param gd_lon
     * @param gd_lat
     * @return
     */
    public 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;
    }
}

使用說明:

 //調用百度地圖客戶端
  if(MapUtil.isInstallByRead("com.baidu.BaiduMap")){
      MapUtil.getBaiduMapUri(this,"39.98871 ","116.43234","39.91441","116.40405");
  }else{
      Toast.makeText(MainActivity.this, "您尚未安裝百度地圖", Toast.LENGTH_LONG).show();
      Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
      Intent intent = new Intent(Intent.ACTION_VIEW, uri);
      startActivity(intent);
  }




//調用高德地圖客戶端
 if (MapUtil.isInstallByRead("com.autonavi.minimap")){                      
     MapUtil.getGaodeMapUri(this,"39.92848272","116.39560823","39.98848272","116.47560823");
 }else{
     Toast.makeText(MainActivity.this, "您尚未安裝高德地圖", Toast.LENGTH_LONG).show();
     Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     startActivity(intent);
}

效果圖

已安裝百度地圖和高德地圖




百度地圖




高德地圖





未安裝百度地圖和高德地圖(調用手機的應用商店進行下載)












好了,就分享到這了!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章