android Intent調(diào)用地圖應(yīng)用客戶端(二)

版權(quán)聲明:這是轉(zhuǎn)載的,這是轉(zhuǎn)載的,這是轉(zhuǎn)載的

原地址:http://blog.csdn.net/u011102153/article/details/52870078

目錄(?)[+]
1.需求
Android應(yīng)用中打開百度地圖或者高德地圖進(jìn)行路線規(guī)劃,如果沒有安裝則打開網(wǎng)頁百度地圖進(jìn)行路線規(guī)劃。
2.API
2.1 打開百度地圖應(yīng)用
地址:http://lbsyun.baidu.com/index.php?title=uri/api/android
打開文檔可以看到功能還是很多的,這里只介紹 公交、駕車、導(dǎo)航、步行和騎行導(dǎo)航

這里寫圖片描述

注:必選項一定要填
2.2 打開瀏覽器并跳轉(zhuǎn)到網(wǎng)頁百度地圖
參數(shù)
這里寫圖片描述

2.3.打開高德地圖應(yīng)用
參數(shù)
這里寫圖片描述

3.使用
封裝了一個工具類,具體請看代碼

public class OpenLocalMapUtil {
  /**
 * 地圖應(yīng)用是否安裝
 * @return
 */
  public static boolean isGdMapInstalled(){
   return isInstallPackage("com.autonavi.minimap");
  }
  public static boolean isBaiduMapInstalled(){
   return isInstallPackage("com.baidu.BaiduMap");
  }
   private static boolean isInstallPackage(String packageName) {
   return new File("/data/data/" + packageName).exists();
  }
/**
 * 獲取打開百度地圖應(yīng)用uri [http://lbsyun.baidu.com/index.php?title=uri/api/android]
 * @param originLat
 * @param originLon
 * @param desLat
 * @param desLon
 * @return
 */
   public static String getBaiduMapUri(String originLat, String originLon, String originName, String desLat, String desLon, String destination, String region, String src){
   String uri = "intent://map/direction?origin=latlng:%1$s,%2$s|name:%3$s" +
           "&destination=latlng:%4$s,%5$s|name:%6$s&mode=driving&region=%7$s&src=%8$s#Intent;" +
           "scheme=bdapp;package=com.baidu.BaiduMap;end";
 return String.format(uri, originLat, originLon, originName, desLat, desLon, destination, region, src);
  }
  /**
 * 獲取打開高德地圖應(yīng)用uri
 */
   public static String getGdMapUri(String appName, String slat, String slon, String sname, String dlat, String dlon, String dname){
   String uri = "androidamap://route?sourceApplication=%1$s&slat=%2$s&slon=%3$s&sname=%4$s&dlat=%5$s&dlon=%6$s&dname=%7$s&dev=0&m=0&t=2";
   return String.format(uri, appName, slat, slon, sname, dlat, dlon, dname);
 }

  /**
 * 網(wǎng)頁版百度地圖 有經(jīng)緯度
 * @param originLat
 * @param originLon
 * @param originName ->注:必填
 * @param desLat
 * @param desLon
 * @param destination
 * @param region : 當(dāng)給定region時,認(rèn)為起點和終點都在同一城市,除非單獨給定起點或終點的城市。-->注:必填,不填不會顯示導(dǎo)航路線
 * @param appName
 * @return
 */
   public static String getWebBaiduMapUri(String originLat, String originLon, String originName, String desLat, String desLon, String destination, String region, String appName) {
   String uri = "http://api.map.baidu.com/direction?origin=latlng:%1$s,%2$s|name:%3$s" +
           "&destination=latlng:%4$s,%5$s|name:%6$s&mode=driving&region=%7$s&output=html" +
           "&src=%8$s";
   return String.format(uri, originLat, originLon, originName, desLat, desLon, destination, region, appName);
  }

   /**
 * 百度地圖定位經(jīng)緯度轉(zhuǎn)高德經(jīng)緯度
 * @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;
   }
/**
 * 高德地圖定位經(jīng)緯度轉(zhuǎn)百度經(jīng)緯度
 * @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;
      }
   }

注:百度地圖和高德地圖使用的坐標(biāo)系不同,但是用上面兩個方法轉(zhuǎn)換的坐標(biāo)還是有問題。測試時把百度地圖定位的坐標(biāo)用上面的方法轉(zhuǎn)換成高德地圖的坐標(biāo),然后在高德地圖網(wǎng)站對轉(zhuǎn)換后的坐標(biāo)進(jìn)行查找,能找到正確的位置,然而打開高德地圖app后提示位置不在支持范圍內(nèi)。幸運的是,高德地圖sdk中有相應(yīng)的工具類CoordinateConverter對坐標(biāo)進(jìn)行轉(zhuǎn)換。
打開百度地圖

  /**
 * 打開百度地圖
 */
   private void openBaiduMap(double slat, double slon, String sname, double dlat, double dlon, String dname, String city) {
   if(OpenLocalMapUtil.isBaiduMapInstalled()){
       try {
           String uri = OpenLocalMapUtil.getBaiduMapUri(String.valueOf(slat), String.valueOf(slon), sname,
                   String.valueOf(dlat), String.valueOf(dlon), dname, city, SRC);
           Intent intent = Intent.parseUri(uri, 0);
           startActivity(intent); //啟動調(diào)用
    isOpened = true;
       } catch (Exception e) {
           isOpened = false;
           e.printStackTrace();
       }
   } else{
       isOpened = false;
   }

}

打開瀏覽器進(jìn)行百度地圖導(dǎo)航

/**

 * 打開瀏覽器進(jìn)行百度地圖導(dǎo)航
 */
   private void openWebMap(double slat, double slon, String sname, double dlat, double dlon, String dname, String city){
   Uri mapUri = Uri.parse(OpenLocalMapUtil.getWebBaiduMapUri(String.valueOf(slat), String.valueOf(slon), sname,
           String.valueOf(dlat), String.valueOf(dlon),
           dname, city, APP_NAME));
   Intent loction = new Intent(Intent.ACTION_VIEW, mapUri);
   startActivity(loction);

}

?

打開高德地圖

   /**
 * 打開高德地圖
 */
   private void openGaoDeMap(double slat, double slon, String sname, double dlat, double dlon, String dname) {
   if(OpenLocalMapUtil.isGdMapInstalled()){
       try {
       //百度地圖定位坐標(biāo)轉(zhuǎn)換成高德地圖可識別坐標(biāo)
           CoordinateConverter converter= new CoordinateConverter(this);
           converter.from(CoordinateConverter.CoordType.BAIDU);
           DPoint sPoint = null, dPoint = null;
           try {
               converter.coord(new DPoint(slat, slon));
               sPoint = converter.convert();
               converter.coord(new DPoint(dlat, dlon));
               dPoint = converter.convert();
           } catch (Exception e) {
               e.printStackTrace();
           }
           if (sPoint != null && dPoint != null) {
               String uri = OpenLocalMapUtil.getGdMapUri(APP_NAME, String.valueOf(sPoint.getLatitude()), String.valueOf(sPoint.getLongitude()),

                       sname, String.valueOf(dPoint.getLatitude()), String.valueOf(dPoint.getLongitude()), dname);
               Intent intent = new Intent(Intent.ACTION_VIEW);
               intent.setPackage("com.autonavi.minimap");
               intent.setData(Uri.parse(uri));
               startActivity(intent); //啟動調(diào)用
   isOpened = true;
           }
       } catch (Exception e) {
           isOpened = false;
           e.printStackTrace();
       }

   } else{
       isOpened = false;
   }
      }

?

結(jié)果圖:


這里寫圖片描述

4.下載
定位沒加

OPenLocalMapDemo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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