baidumap 判断是否安装百度地图客户端

 一下调用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);  
}  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值