/** * Created by zhq_zhao on 2017/5/5. * 注意百度不需要起始位置的经纬度,而高德需要起始位置的经纬度 * 使用第三方导航:高德、百度。。。。。。。。。。 */ public class AmapUtil { private static AmapUtil mAmapUtil; private LocationManager locationManager; private String locationProvider; public static String slat; public static String slng; private Location location; public AmapUtil(Context getApplicationContext) { //获取地理位置管理器 locationManager = (LocationManager) getApplicationContext.getSystemService(Context.LOCATION_SERVICE); //获取所有可用的位置提供器 List<String> providers = locationManager.getProviders(true); if (providers.contains(LocationManager.GPS_PROVIDER)) { //如果是GPS locationProvider = LocationManager.GPS_PROVIDER; } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) { //如果是Network locationProvider = LocationManager.NETWORK_PROVIDER; } else { ToastUtil.show(getApplicationContext, "没有可用的位置提供器"); return; } //获取Location if (ActivityCompat.checkSelfPermission(getApplicationContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ToastUtil.show(getApplicationContext, "没有可用的位置提供器"); } location = locationManager.getLastKnownLocation(locationProvider); if(location !=null){ //不为空,显示地理位置经纬度 getLocationInfo(location,getApplicationContext); } } /**获取起点位置用于高德地图导航的使用 * * @param location */ private void getLocationInfo(Location location,Context context) { if (location != null) { //获取纬度 slat = String.valueOf(location.getLatitude()); //获取经度 slng = String.valueOf(location.getLongitude()); } else { ToastUtil.show(context,"请允许定位权限"); return ; } } /** * 根据包名检测某个APP是否安装 * @param packageName 包名 百度的包名为 com.baidu.BaiduMap,高德com.autonavi.minimap,腾讯 * @return true 安装 false 没有安装 */ public boolean isInstallByRead(String packageName) { return new File("/data/data/" + packageName).exists(); } public static AmapUtil getInstance(Context context){ if(mAmapUtil ==null){ mAmapUtil =new AmapUtil(context); } return mAmapUtil; } /** * 打开百度地图导航客户端 * data = Intent.getIntent("baidumap://map/navi?location=34.264642646862,108.95108518068&type=BLK&src=thirdapp.navi.you * location 坐标点 location与query二者必须有一个,当有location时,忽略query * query 搜索key 同上 * type 路线规划类型 BLK:躲避拥堵(自驾);TIME:最短时间(自驾);DIS:最短路程(自驾);FEE:少走高速(自驾);默认DIS */ public void openBaiduNavi(Context context,String lat,String lng) { StringBuffer stringBuffer = new StringBuffer("baidumap://map/geocoder?location=") .append(lat).append(",").append(lng).append("&type=TIME"); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(stringBuffer.toString())); intent.setPackage("com.baidu.BaiduMap"); context.startActivity(intent); } /** * 打开google Web地图导航 */ public void openWebGoogleNavi(Context context,String lat,String lng) { // http://ditu.google.cn/maps?hl=zh&mrt=loc&q= StringBuffer stringBuffer = new StringBuffer(" http://ditu.google.cn/maps?hl=zh&mrt=loc&q=") .append("to").append(lat).append(",").append(lng); Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(stringBuffer.toString())); context. startActivity(i); } /** * 打开网页版 导航 (高德) * 不填我的位置,则通过浏览器定未当前位置 * * @param context * @param myLatLng 起点经纬度 * @param myAddress 起点地址名展示 * @param destination 终点经纬度 * @param destinationAddress 终点地址名展示 */ public static void openBrowserMap(Context context, LatLonPoint myLatLng, LatLonPoint destination) { Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); intent.setData(Uri.parse("http://uri.amap.com/navigation?" + "from=" + myLatLng.getLongitude() + "," +myLatLng.getLatitude() + "," + "start&to=" + destination.getLongitude() + "," + destination.getLatitude() + "," + "end&mode=car&src=CQRCB")); context.startActivity(intent); } /** * 打开网页版 导航 (百度) * @param activity * @param sLongitude * @param sLatitude * @param sName * @param dLongitude * @param dLatitude * @param dName * @param region * @param appName * @param useOutWeb */ public static void openBrosserNaviMap(Context activity, String sLongitude, String sLatitude, String sName, String dLongitude, String dLatitude, String dName) { Uri mapUri = Uri.parse("http://api.map.baidu.com/direction?origin=latlng:" + sLatitude + "," + sLongitude + "|name:" + sName + "&destination=latlng:" + dLatitude + "," + dLongitude + "|name:" + dName + "&mode=driving®ion=" + "重庆" + "&output=html&src=" + "重庆农商行"); Intent loction = new Intent(Intent.ACTION_VIEW, mapUri); activity.startActivity(loction); } /** * 打开google地图客户端开始导航 * q:目的地 * mode:d驾车 默认 */ public void openGoogleNavi(Context context,String lat,String lng) { StringBuffer stringBuffer = new StringBuffer("google.navigation:q=").append(lat).append(",").append(lng).append("&mode=d"); Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(stringBuffer.toString())); i.setPackage("com.google.android.apps.maps"); context.startActivity(i); } /** * 启动高德App进行导航 * sourceApplication 必填 第三方调用应用名称。如 amap * poiname 非必填 POI 名称 * dev 必填 是否偏移(0:lat 和 lon 是已经加密后的,不需要国测加密; 1:需要国测加密) * style 必填 导航方式(0 速度快; 1 费用少; 2 路程短; 3 不走高速;4 躲避拥堵;5 不走高速且避免收费;6 不走高速且躲避拥堵;7 躲避收费和拥堵;8 不走高速躲避收费和拥堵)) */ public void openGaoDeNavi(Context context,String slat,String slng) { StringBuffer stringBuffer = new StringBuffer("androidamap://navi?sourceApplication=") .append("yitu8_driver").append("&lat=").append(slat) .append("&lon=").append(slng) .append("&dev=").append(1) .append("&style=").append(0); ; // if (!TextUtils.isEmpty(poiname)) { // stringBuffer.append("&poiname=").append(poiname); // } Intent intent = new Intent(Intent.ACTION_VIEW, android.net.Uri.parse(stringBuffer.toString())); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setPackage("com.autonavi.minimap"); context.startActivity(intent); } }