主要工具类
/**
* Created by meixi on 2018/6/29.
* 使用第三方导航:高德、百度。。。。。。。。。。
*/
public class AmapUtil {
public static final String BAIDU = "com.baidu.BaiduMap";
public static final String GAODE = "com.autonavi.minimap";
public static final String TENCENT = "com.tencent.map";
/**
* 启动高德App进行导航
* Version
1.0
* CreateTime
2016/6/27,13:58
* UpdateTime
2016/6/27,13:58
* CreateAuthor
* UpdateAuthor
* UpdateInfo
(此处输入修改内容,若无修改可不写.)
*
* @param sourceApplication 必填 第三方调用应用名称。如 amap
* @param poiname 非必填 POI 名称
* @param lat 必填 纬度
* @param lon 必填 经度
* @param dev 必填 是否偏移(0:lat 和 lon 是已经加密后的,不需要国测加密; 1:需要国测加密)
* @param style 必填 导航方式(0 速度快; 1 费用少; 2 路程短; 3 不走高速;4 躲避拥堵;5 不走高速且避免收费;6 不走高速且躲避拥堵;
* 7 躲避收费和拥堵;8不走高速躲避收费和拥堵))
*/
/** 高德导航 **/
public static void goToGaodeNaviActivity(Context context, String sourceApplication,
String poiname, String lat, String lon, String dev, String style) {
StringBuffer stringBuffer =
new StringBuffer("androidamap://navi?sourceApplication=").append(sourceApplication);
if (!TextUtils.isEmpty(poiname)) {
stringBuffer.append("&poiname=").append(poiname);
}
stringBuffer.append("&lat=")
.append(lat)
.append("&lon=")
.append(lon)
.append("&dev=")
.append(dev)
.append("&style=")
.append(style);
Intent intent =
new Intent("android.intent.action.VIEW", android.net.Uri.parse(stringBuffer.toString()));
intent.setPackage(GAODE);
context.startActivity(intent);
}
/**
* @param slat 起点
* @param dlat 终点
* @param dev 必填 是否偏移(0:lat 和 lon 是已经加密后的,不需要国测加密; 1:需要国测加密)
* @param t t = 1(公交) =2(驾车) =4(步行)
* @param showType 高德导航
* /*"androidamap://route?sourceApplication=changanchuxing"
* + "&slat=36.2&slon=116.1&sname=abc&dlat=36.3"
* + "&dlon=116.2&dname=def&dev=0&m=0&t=1&showType=1"
* 高德开放平台 http://lbs.amap.com/api/uri-api/guide/android-uri-explain/route/
*/
public static void goToGaodeNaviActivity(Context context, String sourceApplication, String slat,
String slon, String sname, String dlat, String dlon, String dname, String dev, String m,
String t, String showType) {
String locationStr = "androidamap://route?sourceApplication="
+ sourceApplication
+
"&slat="
+ slat
+ "&slon="
+ slon
+
"&sname="
+ sname
+ "&dlat="
+ dlat
+ "&dlon="
+ dlon
+ "&dname="
+ dname
+ "&dev="
+ dev
+ "&m="
+ m
+ "&t="
+ t
+ "&showType="
+ showType;
Intent intent = new Intent("android.intent.action.VIEW",
android.net.Uri.parse(/*stringBuffer.toString()*/locationStr));
intent.setPackage(GAODE);
context.startActivity(intent);
}
/**
* 启动BaiduApp进行导航
* Version
1.0
* CreateTime
2016/6/27,11:23
* UpdateTime
2016/6/27,11:23
* CreateAuthor
luzhenbang
* UpdateAuthor
* UpdateInfo
(此处输入修改内容,若无修改可不写.)
*
* @param context 上下文
* @param origin 必选 起点名称或经纬度,或者可同时提供名称和经纬度,此时经纬度优先级高,将作为导航依据,名称只负责展示。例如:
* latlng:34.264642646862,108.95108518068|name:我家
* @param destination 必选 终点名称或经纬度,或者可同时提供名称和经纬度,此时经纬度优先级高,将作为导航依据,名称只负责展示。
* @param mode 必选 导航模式,固定为transit、driving、walking,分别表示公交、驾车和步行
* @param region 必选 城市名或县名 当给定region时,认为起点和终点都在同一城市,除非单独给定起点或终点的城市。
* @param origin_region 必选 起点所在城市或县
* @param destination_region 必选 终点所在城市或县
* @param coord_type 可选 坐标类型,可选参数,默认为bd09经纬度坐标。
* @param zoom 可选 展现地图的级别,默认为视觉最优级别。
* @param src 必选 调用来源,规则:companyName|appName。
*/
/** 百度导航 */
public static void goToBaiduNaviActivity(Context context, String origin, String destination,
String mode, String region, String origin_region, String destination_region,
String coord_type, String zoom, String src) {
StringBuffer stringBuffer = new StringBuffer("intent://map/direction?origin=");
stringBuffer.append(origin)
.append("&destination=")
.append(destination)
.append("&mode=")
.append(mode);
if (!TextUtils.isEmpty(region)) {
stringBuffer.append("®ion=").append(region);
}
if (!TextUtils.isEmpty(origin_region)) {
stringBuffer.append("&origin_region=").append(origin_region);
}
if (!TextUtils.isEmpty(destination_region)) {
stringBuffer.append("&destination_region=").append(destination_region);
}
if (!TextUtils.isEmpty(coord_type)) {
stringBuffer.append("&coord_type=").append(coord_type);
}
if (!TextUtils.isEmpty(zoom)) {
stringBuffer.append("&zoom=").append(zoom);
}
stringBuffer.append("&src=")
.append(src)
.append("#Intent;scheme=bdapp;package=" + BAIDU +";end");
String intentString = stringBuffer.toString();
try {
Intent intent = Intent.getIntent(intentString);
context.startActivity(intent);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
/***
* @param originlat 起点维度
* @param originlon 起点经度
* @param destinationlat 终点
* @param mode 出行方式 导航模式,固定为transit(公交)、 driving(驾车)、walking(步行)和riding(骑行). 默认:driving
* 具体看百度地图开放者平台官网 http://lbsyun.baidu.com/index.php?title=uri/api/android
*/
/*// 公交路线规划
//i1.setData(Uri.parse("baidumap://map/direction?origin=name:对外经贸大学|latlng:39.98871,116.43234&destination=name:西直门&mode=transit&sy=3&index=0&target=1"));
// 驾车路线规划
i1.setData(Uri.parse("baidumap://map/direction?region=beijing&origin=39.98871,116.43234&destination=name:西直门&mode=driving"));
// 步行路线规划
i1.setData(Uri.parse("baidumap://map/direction?region=beijing&origin=39.98871,116.43234&destination=40.057406655722,116.2964407172&mode=walking"));
startActivity(i1);*/
public static void goToBaiduNaviActivity(Context context, double originlat, double originlon,
double destinationlat, double destinationlon, String mode) {
String locationStr = "baidumap://map/direction?origin="
+ GCJ02ToBD09(originlon, originlat)[1]
+ ","
+ GCJ02ToBD09(originlon, originlat)[0]
+ "&destination="
+ GCJ02ToBD09(destinationlon, destinationlat)[1]
+ ","
+ GCJ02ToBD09(destinationlon, destinationlat)[0]
+ "&mode="
+ mode;
Intent intent = new Intent("android.intent.action.VIEW",
android.net.Uri.parse(/*stringBuffer.toString()*/locationStr));
intent.setPackage(BAIDU);
context.startActivity(intent);
}
/** 腾讯地图 Uri 标识*/
public final static String BASE_URL = "qqmap://map/";
/**
* 调用腾讯地图app驾车导航
* (此处输入方法执行任务.)
* Version
1.0
* CreateTime
2017/11/9,15:31
* UpdateTime
2017/11/9,15:31
* CreateAuthor
* UpdateAuthor
* UpdateInfo
(此处输入修改内容,若无修改可不写.)
* @param context
* @param from 选 出发地址
* @param fromcoord 选 出发经纬度 移动端如果起点名称和起点坐标均未传递,则使用当前定位位置作为起点 如 39.9761,116.3282
* @param to 必 目标地址
* @param tocoord 必 目标经纬度 39.9761,116.3282
* @param policy 选 本参数取决于type参数的取值
* 公交:type=bus,policy有以下取值 0:较快捷 1:少换乘 2:少步行 3:不坐地铁
* 驾车:type=drive,policy有以下取值 0:较快捷 1:无高速 2:距离 policy的取值缺省为0
* @param coord_type 选 坐标类型,取值如下:1 GPS 2 腾讯坐标(默认) 如果用户指定该参数为非腾讯地图坐标系,则URI API自动进行坐标处理,以便准确对应到腾讯地图底图上。
* @param type 必 公交:bus 驾车:drive 步行:walk(仅适用移动端)
* @param referer 必 调用来源,一般为您的应用名称,为了保障对您的服务,请务必填写!
*/
public static void invokeNavi(Context context, @NonNull String type, String coord_type, String from ,
String fromcoord, @NonNull String to, @NonNull String tocoord, String policy, @NonNull String referer){
StringBuffer stringBuffer = new StringBuffer(BASE_URL)
.append("routeplan?")
.append("type=")
.append(type)
.append("&to=")
.append(to)
.append("&tocoord=")
.append(tocoord)
.append("&referer=")
.append(referer);
if (!TextUtils.isEmpty(from)){
stringBuffer.append("&from=").append(from);
}
if (!TextUtils.isEmpty(fromcoord)){
stringBuffer.append("&fromcoord=").append(fromcoord);
}
if (!TextUtils.isEmpty(policy)){
stringBuffer .append("&policy=").append(policy);
}
if (!TextUtils.isEmpty(coord_type)){
stringBuffer .append("&coord_type=").append(coord_type);
}
Intent intent = new Intent();
intent.setData(Uri.parse(stringBuffer.toString()));
context.startActivity(intent);
}
/**
* file:///E:/someDownCode/IntentMapGuide-master/%E8%85%BE%E8%AE%AF%E5%9C%B0%E5%9B%BE_URI%E8%A7%84%E8%8C%83%E6%96%87%E6%A1%A3_150417/URI%E8%A7%84%E8%8C%83%E6%96%87%E6%A1%A3.html
* qqmap://map/routeplan?type=drive&from=天坛南门&fromcoord=39.873145,116.413306&to=国家大剧院&tocoord=39.907380,116.388501
* coord=39.904956,116.389449 lat<纬度>,lng<经度>
* //移动端启动腾讯地图App,并显示从出发点[天坛南门] 到 [目的地坐标(国家大剧院)] 的驾车路线规划
* @param context
* @param type 路线规划方式参数:公交 bus 驾车 drive 步行 walk
* @param from
* @param fromLat
* @param fromLon
* @param to
* @param toLat
* @param toLon
*/
public static void goToTenCentNaviActivity(Context context, String type, String from,
double fromLat, double fromLon, String to, double toLat, double toLon) {
String locationStr = "qqmap://map/routeplan?type="
+ type
+"&from="
+ from
+ "&fromcoord="
+ fromLat
+ ","
+ fromLon
+ "&to="
+ to
+ "&tocoord="
+ toLat
+ ","
+ toLon;
System.out.println("tencent-location:" + locationStr);
Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse(locationStr));
intent.setPackage(TENCENT);
context.startActivity(intent);
}
/**
* 根据包名检测某个APP是否安装
* Version
1.0
* CreateTime
2016/6/27,13:02
* UpdateTime
2016/6/27,13:02
* CreateAuthor
* UpdateAuthor
* UpdateInfo
(此处输入修改内容,若无修改可不写.)
*
* @param packageName 包名 百度的包名为 com.baidu.BaiduMap,高德com.autonavi.minimap,腾讯
* @return true 安装 false 没有安装
*/
public static boolean isInstallByRead(String packageName) {
return new File("/data/data/" + packageName).exists();
}
/*
* 检查手机上是否安装了指定的软件
* @param context
* @param packageName:应用包名
* @return
*/
public static List isAvilibleList(Context context){
//获取packagemanager
final PackageManager packageManager = context.getPackageManager();
//获取所有已安装程序的包信息
List packageInfos = packageManager.getInstalledPackages(0);
//用于存储所有已安装程序的包名
List packageNames = new ArrayList();
//从pinfo中将包名字逐一取出,压入pName list中
if(packageInfos != null){
for(int i = 0; i < packageInfos.size(); i++){
String packName = packageInfos.get(i).packageName;
if (packName.equals(BAIDU)) {
packageNames.add(BAIDU);
} else if (packName.equals(GAODE)) {
packageNames.add(GAODE);
}else if (packName.equals(TENCENT)) {
packageNames.add(TENCENT);
}
}
}
//判断packageNames中是否有目标程序的包名,有TRUE,没有FALSE
return packageNames;
}
/**
* 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
* 即谷歌、高德 转 百度
* 腾讯和高德经纬度一样
**经纬度转换 http://blog.csdn.net/meegomeego/article/details/39927017
* @param gcj_lon
* @param gcj_lat
* @return Double[lon, lat]
*CoordtransformUtil文件坐标转换
*/
private static double x_PI = 3.14159265358979324 * 3000.0 / 180.0;
public static Double[] GCJ02ToBD09(Double gcj_lon, Double gcj_lat) {
double z =
Math.sqrt(gcj_lon * gcj_lon + gcj_lat * gcj_lat) + 0.00002 * Math.sin(gcj_lat * x_PI);
double theta = Math.atan2(gcj_lat, gcj_lon) + 0.000003 * Math.cos(gcj_lon * x_PI);
Double[] arr = new Double[2];
arr[0] = z * Math.cos(theta) + 0.0065;
arr[1] = z * Math.sin(theta) + 0.006;
return arr;
}
/**
* 调用高德导航
*/
/*public void aMap(View view){
if (AMapUtil.isInstallByRead("com.autonavi.minimap")){
AMapUtil.goToNaviActivity(this,"test",null,"34.264642646862","108.95108518068","1","2");
}
}*/
}
1、两个动画
2、style
3、dialoglayout
4、showDialog
/**
*hxw
* 弹框显示导航地图
*
* */
public void showMapDialog(final String startName, final String endName, final double originlat, final double originlon, final double deslat, final double deslon) {
final Dialog dialog;
LayoutInflater inflater = LayoutInflater.from(MarkerActivity.this);
View view = inflater.inflate(R.layout.navigation_dialog, null);
LinearLayout baiduLayout = (LinearLayout) view.findViewById(R.id.baiduLayout);
LinearLayout gaodeLayout = (LinearLayout) view.findViewById(R.id.gaodeLayout);
LinearLayout tencentLayout = (LinearLayout) view.findViewById(R.id.tencentLayout);
TextView dismiss = (TextView)view.findViewById(R.id.dismiss);
dialog = new Dialog(MarkerActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
Window dialogWindow = dialog.getWindow();
dialogWindow.setGravity(Gravity.BOTTOM);
dialogWindow.setWindowAnimations(R.style.AnimViewshow);
// dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
// dialogWindow.setGravity(Gravity.CENTER);
// lp.width = 900; // 宽度
// lp.height = 650; // 高度
// lp.alpha = 0.7f; // 透明度
dialogWindow.setAttributes(lp);
dialog.setCanceledOnTouchOutside(true);
List installedMapList = AmapUtil.isAvilibleList(MarkerActivity.this);
if (installedMapList.size() == 0) {
Toast.makeText(MarkerActivity.this, "请安装导航地图软件", Toast.LENGTH_LONG).show();
return;
}
for (String map :installedMapList) {
if (map.equals(AmapUtil.BAIDU)) {
baiduLayout.setVisibility(View.VISIBLE);
}else if (map.equals(AmapUtil.GAODE)) {
gaodeLayout.setVisibility(View.VISIBLE);
} else if (map.equals(AmapUtil.TENCENT)) {
tencentLayout.setVisibility(View.VISIBLE);
}
}
StringBuilder sb = new StringBuilder();
baiduLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AmapUtil.goToBaiduNaviActivity(MarkerActivity.this, originlat, originlon, deslat, deslon, "driving");
dialog.dismiss();
}
});
gaodeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AmapUtil.goToGaodeNaviActivity(MarkerActivity.this, "yxbl","", String.valueOf(deslat), String.valueOf(deslon), "0", "0");
dialog.dismiss();
}
});
tencentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// AmapUtil.goToTenCentNaviActivity(getContext(), "drive", startName, originlat, originlon, endName, deslat, deslon);
String toc = String.valueOf(deslat)+","+String.valueOf(deslon);
// AmapUtil.invokeNavi(mContext,"drive",null,null,null,"故宫","39.9761,116.3282",null,"textApp");
AmapUtil.invokeNavi(mContext,"drive",null,null,null,endName,toc,null,"textApp");
dialog.dismiss();
}
});
dismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
5、调用
showMapDialog("起点", string, latitude, longitude, lawd, lojd );
//string、起点位置如:体育路5号,latitude起点纬度,longitude起点经度,lawd终点纬度,lojd终点经度
private static final String GAODE_PACKAGE = "com.autonavi.minimap";
工具方法:
private void startGaodeNavi(double latitude, double longitude) { Intent intents = new Intent(); intents.setData(Uri.parse("androidamap://navi?sourceApplication=softname&lat=" + latitude + "&lon=" + longitude + "&dev=0")); startActivity(intents); }
private boolean checkApkExist(Context context, String packageName) { if (TextUtils.isEmpty(packageName)) { return false; } try { context.getPackageManager() .getApplicationInfo(packageName, PackageManager.MATCH_UNINSTALLED_PACKAGES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } }
private void navigate(double longitude, double latitude) { if (checkApkExist(this, GAODE_PACKAGE)) { startGaodeNavi(latitude, longitude); } else { //showOneToast(R.string.qingxiananzhuanggaodeditu); ToastUtil.showToastLong(this, "请安装高德app,此刻正打开网页版"); Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); intent.setData(Uri.parse("http://uri.amap.com/navigation?" + "to=" + longitude + "," + latitude + "&mode=car&src=nyx_super")); startActivity(intent); } }
启动数据有效
navigate(109.650222,23.05315);