记录
知道经纬度,去这个地方去的路线规划,调用第三方的app(如百度,高德,腾讯),记录一下,下次就可以直接用,而不是又要去百度搜,避免一些重复性的操作,来提高效率。
添加打开浏览器调高德地图
参考网址
具体搜索:【平台名】+ URI API
高德地图:http://lbs.amap.com/api/amap-mobile/guide/android/route
百度地图:http://lbsyun.baidu.com/index.php?title=uri/api/android
腾讯地图:http://lbs.qq.com/uri_v1/guide-route.html
使用前
注意:调用之前需要确认是否安装了对应的 地图app
使用
高德地图
关注红色圈出来的和必填的就可以了
public static void openGaodeMap(Context context, LatLng myLatlng, String myAddress, LatLng destination,
String destinationAddress) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("amapuri://route/plan/?" +
"slat=" + myLatlng.latitude +
"&slon=" + myLatlng.longitude +
"&sname=" + myAddress +
"&dlat=" + destination.latitude +
"&dlon=" + destination.longitude +
"&dname=" + destinationAddress +
"&dev=0" +
"&t=0"));
context.startActivity(intent);
}
//如果只是从当前位置导航到其他位置,可以不传自己的经纬度。
intent.setData(Uri.parse("amapuri://route/plan/?" +
"dlat=" + destination.latitude +
"&dlon=" + destination.longitude +
"&dname=" + destinationAddress +
"&dev=0" +
"&t=0"));
context.startActivity(intent);
百度地图
/**
* 调用百度地图
* @param myLocation 起点经纬度
* @param myAddress 起点地址
* @param destination 目的地经纬度
* @param destinationAddress 目的地地址
*/
public static void openBaiduMap(Context context, LatLng myLocation, String myAddress, LatLng destination,
String destinationAddress) {
Intent intent = new Intent();
intent.setData(Uri.parse("baidumap://map/direction?" +
"origin=latlng:" + myLocation.latitude + "," + myLocation.longitude +
"|name:" + myAddress +
"&destination=latlng:" + destination.latitude + "," + destination.longitude +
"|name:" + destinationAddress +
"&mode=driving"));
context.startActivity(intent);
}
同样的,自己的位置为起点,就可以改为下个方式
/**
* 调用百度地图
* @param destination 目的地经纬度
* @param destinationAddress 目的地地址
*/
public static void openBaiduMap(Context context, LatLng destination,
String destinationAddress) {
Intent intent = new Intent();
intent.setData(Uri.parse("baidumap://map/direction?" +
"destination=latlng:" + destination.latitude + "," + destination.longitude +
"|name:" + destinationAddress +
"&mode=driving"));
context.startActivity(intent);
}
腾讯地图
/**
* 调用腾讯地图
*
* @param context
* @param destination 目的地经纬度
* @param destinationAddress 目的地地址
* 腾讯地图参考网址:http://lbs.qq.com/uri_v1/guide-route.html
*/
public static void openTentcentMap(Context context, LatLng destination, String destinationAddress) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setData(Uri.parse("qqmap://map/routeplan?" +
"type=drive" +
"&from=" +
"&fromcoord=" +
"&to=" + destinationAddress +
"&tocoord=" + destination.latitude + "," + destination.longitude +
"&policy=0" +
"&referer=appName"));
context.startActivity(intent);
}
//from:起始位置名
//fromcoord:起始位置经纬度,“,”隔开
浏览器导航
/**
* 打开网页版 导航
* @param context
* @param myLatLng 起点经纬度
* @param myAddress 起点地址名展示
* @param destination 终点经纬度
* @param destinationAddress 终点地址名展示
*/
public static void openBrowserMap(Context context, LatLng myLatLng, String myAddress, LatLng destination,
String destinationAddress) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setData(Uri.parse("http://uri.amap.com/navigation?" +
"from=" + myLatLng.longitude + "," + myLatLng.latitude + "," + myAddress +
"to=" + destination.longitude + "," + destination.latitude + "," + destinationAddress +
"&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0"));
context.startActivity(intent);
}
工具类
public class MapUtils {
private static final String BAIDU_PACKAGE_NAME = "com.baidu.BaiduMap";
private static final String GAODE_PACKAGE_NAME = "com.autonavi.minimap";
private static final String TENCENT_PACKAGE_NAME = "com.tencent.map";
/**
* 是否安装百度地图
*/
public static boolean haveBaiduMap(Context context) {
return exist(context, BAIDU_PACKAGE_NAME);
}
public static boolean haveGaodeMap(Context context) {
return exist(context, GAODE_PACKAGE_NAME);
}
public static boolean haveTencentMap(Context context) {
return exist(context, TENCENT_PACKAGE_NAME);
}
/**
* 检查手机上是否安装了指定的软件
*
* @param context
* @param packageName:应用包名
* @return true 存在
*/
public static boolean exist(Context context, String packageName) {
//获取packagemanager
final PackageManager packageManager = context.getPackageManager();
//获取所有已安装程序的包信息
List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
//用于存储所有已安装程序的包名
List<String> packageNames = new ArrayList<>();
//从pinfo中将包名字逐一取出,压入pName list中
if (packageInfos != null) {
for (int i = 0; i < packageInfos.size(); i++) {
String packName = packageInfos.get(i).packageName;
packageNames.add(packName);
}
}
//判断packageNames中是否有目标程序的包名,有true,没有false
return packageNames.contains(packageName);
}
/**
* 调用高德地图app,导航
*
* @param context 上下文
* @param destination 目标经纬度
* @param destinationAddress 目标地址
* 高德地图:http://lbs.amap.com/api/amap-mobile/guide/android/route
*/
public static void openGaodeMap(Context context, LatLng destination,
String destinationAddress) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("amapuri://route/plan/?" +
"dlat=" + destination.latitude +
"&dlon=" + destination.longitude +
"&dname=" + destinationAddress +
"&dev=0" +
"&t=0"));
context.startActivity(intent);
}
/**
* 调用百度地图
*
* @param destination 目的地经纬度
* @param destinationAddress 目的地地址
* 百度参考网址:http://lbsyun.baidu.com/index.php?title=uri/api/android
*/
public static void openBaiduMap(Context context, LatLng destination,
String destinationAddress) {
Intent intent = new Intent();
intent.setData(Uri.parse("baidumap://map/direction?" +
"destination=latlng:" + destination.latitude + "," + destination.longitude +
"|name:" + destinationAddress +
"&mode=driving"));
context.startActivity(intent);
}
/**
* 调用腾讯地图
*
* @param context
* @param destination 目的地经纬度
* @param destinationAddress 目的地地址
* 腾讯地图参考网址:http://lbs.qq.com/uri_v1/guide-route.html
*/
public static void openTentcentMap(Context context, LatLng destination, String destinationAddress) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setData(Uri.parse("qqmap://map/routeplan?" +
"type=drive" +
"&from=" +
"&fromcoord=" +
"&to=" + destinationAddress +
"&tocoord=" + destination.latitude + "," + destination.longitude +
"&policy=0" +
"&referer=appName"));
context.startActivity(intent);
}
/**
* 打开网页版 导航
* 不填我的位置,则通过浏览器定未当前位置
*
* @param context
* @param myLatLng 起点经纬度
* @param myAddress 起点地址名展示
* @param destination 终点经纬度
* @param destinationAddress 终点地址名展示
*/
public static void openBrowserMap(Context context, LatLng myLatLng, String myAddress, LatLng destination,
String destinationAddress) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setData(Uri.parse("http://uri.amap.com/navigation?" +
"from=" + myLatLng.longitude + "," + myLatLng.latitude + "," + myAddress +
"to=" + destination.longitude + "," + destination.latitude + "," + destinationAddress +
"&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0"));
context.startActivity(intent);
}
}