一下调用andrid 百度地图的Intent
方法一:
try {
intent = Intent.getIntent("intent://map/direction?origin=latlng:34.264642646862,108.95108518068|name:我家&destination=大雁塔&mode=driving®ion=西安&referer=Autohome|GasStation#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
if(isInstallByread("com.baidu.BaiduMap")){
startActivity(intent); //启动调用
Log.e("GasStation", "百度地图客户端已经安装") ;
}else{
Log.e("GasStation", "没有安装百度地图客户端") ;
}
} catch (URISyntaxException e) {
e.printStackTrace();
}
但是,若客户端没有安装了百度地图客户端就会报 No Activity.... 直接会导致程序挂掉,这是开发者不希望看到的.所以在调用之前判断该手机是否安装了百度地图客户端,我们知道百度的包名为 com.baidu.BaiduMap
所以通过下面的方法进行判断:
/**
* 判断是否安装目标应用
* @param packageName 目标应用安装后的包名
* @return 是否已安装目标应用
*/
private boolean isInstallByread(String packageName) {
return new File("/data/data/" + packageName).exists();
}
方法二:
Intent intent = null;
try {// 如果有安装百度地图 就启动百度地图
StringBuffer sbs = new StringBuffer();
sbs.append("intent://map/direction?origin=latlng:")
// 我的位置
.append(latitude)
.append(",")
.append(longitude)
.append("|name:")
.append(getResources().getString(R.string.location))
// 去的位置
.append("&destination=latlng:")
.append(overlayItem.getPoint().getLatitudeE6() / 1E6) // 经度
.append(",")
.append(overlayItem.getPoint().getLongitudeE6() / 1E6)// 纬度
.append("|name:")
.append(overlayItem.getSnippet().replaceAll("nbsp;|&", ""))
// 城市
.append("&mode=driving®ion=")
.append(App.area_name)
.append("&referer=com.menu|menu#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
try {
intent = Intent.getIntent(sbs.toString());
} catch (URISyntaxException e) {
e.printStackTrace();
}
startActivity(intent);
} catch (Exception e) {// 没有百度地图则弹出网页端
StringBuffer sb = new StringBuffer();
sb.append("http://api.map.baidu.com/direction?origin=latlng:")
// 我的位置
.append(latitude)
.append(",")
.append(longitude)
.append("|name:")
.append(getResources().getString(R.string.location))
// 去的位置
.append("&destination=latlng:")
.append(overlayItem.getPoint().getLatitudeE6() / 1E6)
.append(",")
.append(overlayItem.getPoint().getLongitudeE6() / 1E6)
.append("|name:")
.append(overlayItem.getSnippet().replaceAll("nbsp;|&", ""))
// 城市
.append("&mode=driving®ion=").append(App.area_name)
.append("&output=html");
Uri uri = Uri.parse(sb.toString());
intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}