本文主要是介绍Android App跳转至百度,高德App的驾车(路线)规划,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前段时间做要做个功能,判断手机是否装有百度,高德App,安装哪个弹出哪个,点击地图后跳转至App的驾车路线规划部分。
做这个功能其实本身不难,就是这其中需求各种改动,有些东西没处理好。
效果图:
过程:写一个PopupWindow,写布局,判断手机是否装有地图app,有则展示,没有不展示。集成百度地图获取到当前手机位置的经纬度,然后获取后台数据时每条数据都有目的地地址的经纬度,都是用的百度坐标系的经纬度。点击哪个地图后根据当前经纬度和目的地经纬度,目的地位置进行驾车路线规划。点击高德地图时先把将百度用的坐标系经纬度转换为高德用的坐标系经纬度。
判断是否装有哪个软件,传入软件的包名:
/*** Created by fsx on 2017/7/24.* 判断是否安装某一软件*/
public class AppInstalledUtils {public static boolean isAppInstalled(Context context, String packageName) {PackageManager packageManager = context.getPackageManager();//获取所有已安装程序的包信息List<PackageInfo> pInfo = packageManager.getInstalledPackages(0);//存储所有已安装程序的包名List<String> pName = new ArrayList<>();//从info中将报名字逐一取出if (pInfo != null) {for (int i = 0; i < pInfo.size(); i++) {String pn = pInfo.get(i).packageName;pName.add(pn);}}return pName.contains(packageName);}
}
高德:
AppInstalledUtils.isAppInstalled(getContext(), "com.autonavi.minimap")
百度:
AppInstalledUtils.isAppInstalled(getContext(), "com.baidu.BaiduMap")
坐标的实体类(get set有删减):
/*** Created by fsx on 2017/8/24.* 坐标*/
public class MyLatLngPoint {private double lat;private double lng;public MyLatLngPoint(double lat, double lng) {this.lat = lat;this.lng = lng;}
}
转换坐标的实体类
private final static double PI = 3.14159265358979324;
private final static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;/*** 百度转国测局* 百度转国策局* @param point* @return*/public static MyLatLngPoint bd2gcj(MyLatLngPoint point) {double bdLon = point.getLng();double bdLat = point.getLat();double x = bdLon - 0.0065;double y = bdLat - 0.006;double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);double gcjLon = z * Math.cos(theta);double gcjLat = z * Math.sin(theta);return new MyLatLngPoint(gcjLat, gcjLon);//{'lat' : gcjLat, 'lon' : gcjLon};}
打开高德地图的驾车路线规划:
/*** 打开高德*/private void openGaoDeMap() {intent = new Intent();intent.setAction(Intent.ACTION_VIEW);intent.addCategory(Intent.CATEGORY_DEFAULT);//获取本地存储的百度经纬度lon = Double.valueOf((String) SPUtils.get(MyApplication.getContext(), "lon", new String("4.9E-324")));lat = Double.valueOf((String) SPUtils.get(MyApplication.getContext(), "lat", new String("4.9E-324")));MyLatLngPoint point = null;if (lon != 4.9E-324 && lat != 4.9E-324) {point = new MyLatLngPoint(lat, lon);point = CoordMath.bd2gcj(point);} else {lat = 28.208915;lon = 112.985274;point = new MyLatLngPoint(lat,lon);}MyLatLngPoint point1 = null;if(shopLat!=0 && shopLon!=0){point1 = new MyLatLngPoint(shopLat, shopLon);point1 = CoordMath.bd2gcj(point1);}Uri uri = Uri.parse("amapuri://route/plan/?sid=BGVIS1&slat=" + point.getLat() + "&slon=" + point.getLng() + "&sname=" + MyUrl.getAddressMessage() + "&did=BGVIS2&dlat="+point1.getLat()+"&dlon="+point1.getLng()+"&dname="+address+"&dev=0&t=0");intent.setData(uri);//启动该页面即可mActivity.startActivity(intent);//将popupWindow dismiss掉mPopup.dismiss();}
打开百度地图的驾车路线规划:
/*** 打开百度地图的驾车路线规划界面*/private void openBaiDuMap() {intent = new Intent();lon = Double.valueOf((String) SPUtils.get(MyApplication.getContext(), "lon", new String("4.9E-324")));lat = Double.valueOf((String) SPUtils.get(MyApplication.getContext(), "lat", new String("4.9E-324")));if (lon != 4.9E-324 && lat != 4.9E-324) {intent.setData(Uri.parse("baidumap://map/direction?region=湖南&origin=" + lat + "," + lon + "&destination=" + address + "&mode=driving"));} else {intent.setData(Uri.parse("baidumap://map/direction?region=湖南&origin=" + MyUrl.getAddressMessage() + "&destination=" + address + "&mode=driving"));}mActivity.startActivity(intent);mPopup.dismiss();}
上述url中各参数的具体意义,参考:
百度地图URI API
高德开放平台路线规划
这篇关于Android App跳转至百度,高德App的驾车(路线)规划的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!