版權(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)航
注:必選項(xiàng)一定要填
2.2 打開瀏覽器并跳轉(zhuǎn)到網(wǎng)頁百度地圖
參數(shù)
2.3.打開高德地圖應(yīng)用
參數(shù)
3.使用
封裝了一個(gè)工具類,具體請(qǐng)看代碼
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®ion=%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時(shí)允蜈,認(rèn)為起點(diǎn)和終點(diǎn)都在同一城市冤吨,除非單獨(dú)給定起點(diǎn)或終點(diǎn)的城市。-->注:必填饶套,不填不會(huì)顯示導(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®ion=%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)系不同漩蟆,但是用上面兩個(gè)方法轉(zhuǎn)換的坐標(biāo)還是有問題。測(cè)試時(shí)把百度地圖定位的坐標(biāo)用上面的方法轉(zhuǎn)換成高德地圖的坐標(biāo)妓蛮,然后在高德地圖網(wǎng)站對(duì)轉(zhuǎn)換后的坐標(biāo)進(jìn)行查找怠李,能找到正確的位置,然而打開高德地圖app后提示位置不在支持范圍內(nèi)蛤克。幸運(yùn)的是捺癞,高德地圖sdk中有相應(yīng)的工具類CoordinateConverter對(duì)坐標(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); //啟動(dòng)調(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)換成高德地圖可識(shí)別坐標(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); //啟動(dòng)調(diào)用
isOpened = true;
}
} catch (Exception e) {
isOpened = false;
e.printStackTrace();
}
} else{
isOpened = false;
}
}
?
結(jié)果圖:
4.下載
定位沒加