(一)百度导航sdk(即app内部导航功能):
以下几个步骤需注意:
1.若是包含地图,定位功能,则需要集成功能的jar
http://lbsyun.baidu.com/sdk/download?selected=navifunctionwithtts
2.申请百度key时,填写正确(调试和发布)的sha1值, http://lbsyun.baidu.com/apiconsole/key
(ps:sha1值和md5值问题,参考http://blog.csdn.net/hexingen/article/details/51799515)
3.填写tts白名单(此步骤没弄好,会导致导航没有声音)
http://app.navi.baidu.com/ttsregister/create
4.其他步骤比较简单按照开发指南便可以实现:http://lbsyun.baidu.com/index.php?title=android-navsdk/guide/introduction
5.吐槽下百度导航sdk:
开发工具:android studio 2.1
问题描述:
运行在5.0系统以上,会造成,找不到ClassNotFoundException的问题
(或者java.lang.ClassNotFoundException: Didn't find class "com.gos.navi.BNGuideActivity" on path: DexPathList[[zip file "/data/app/com.gos.navi-2/base.apk")。
运行在 5.0系统的手机上运行没有问题,这是很奇妙的事情。
也有专业人士解答过这个问题:百度工程师,提出关闭as 的instant run的处理方式,然并卵
最为奇妙的是:eclipse上运行项目完成没有问题
(二)调用百度地图客户端导航:
需求:调用百度地图导航,用户自行选择导航方
解决方式:调用百度地图的路线规划,然后进行导航:BaiduMapRoutePlan.openBaiduMapDrivingRoute(routeParaOption, context);
protected void openBaiduNavi() {
LatLng startLatLng = null, endLatLng = null;
startLatLng = new LatLng(BaseApplication.getContext().latitude,
BaseApplication.getContext().longtitude);
endLatLng = new LatLng(tagLatitude, tagLongtitude);
RouteParaOption routeParaOption = new RouteParaOption();
routeParaOption.startPoint(startLatLng);
routeParaOption.endPoint(endLatLng);
try {
BaiduMapRoutePlan
.openBaiduMapDrivingRoute(routeParaOption, context);
} catch (BaiduMapAppNotSupportNaviException e) {
e.printStackTrace();
showToast("请安装百度地图");
}
}
(三)调用高德地图客户端导航:
需求:调用高德地图进行路线规划导航
解决方式:先将百度地图坐标转换为高德地图坐标,然后进行路线规划导航
protected void openAmapNavi() {
// 先转换到高德地图的坐标
com.amap.api.maps.model.LatLng tagLatLng = converter(new com.amap.api.maps.model.LatLng(
tagLatitude, tagLongtitude));
com.amap.api.maps.model.LatLng startLatLng = converter(new com.amap.api.maps.model.LatLng(
BaseApplication.getContext().latitude,
BaseApplication.getContext().longtitude));
try {
RoutePara routePara = new RoutePara();
//注意点:以下参数为必填项,查看其jar中的代码,发现少一参数就给你报非法参数
routePara.setStartPoint(startLatLng);
routePara.setEndPoint(tagLatLng);
routePara.setEndName(tagAdres);
routePara.setStartName(BaseApplication.getContext().address);
AMapUtils.openAMapDrivingRoute(routePara,
BaseApplication.getContext());
} catch (com.amap.api.maps.AMapException e) {
e.printStackTrace();
showToast("请安装高德地图");
/* // 如果没安装会进入异常,调起下载页面
// AMapUtils.getLatestAMapApp(getApplicationContext());
*/ } catch (Exception e) {
e.printStackTrace();
}
}
public com.amap.api.maps.model.LatLng converter(
com.amap.api.maps.model.LatLng sourceLatLng) {
CoordinateConverter converter = new CoordinateConverter(context);
converter.from(CoordType.BAIDU);
converter.coord(sourceLatLng);
return converter.convert();
}
(四)完整案列:集成百度导航,高德导航的例子
将高德,百度地图的导航功能封装到dialog中,代码如下:
1.对应xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" >
<TextView
android:id="@+id/select_navi_top_title"
android:layout_width="match_parent"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/shape_select_navi_top"
android:text="选择地图"
android:textColor="@color/white"
android:layout_alignParentTop="true"
android:gravity="center"
android:textSize="18sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/select_navi_to_cacle_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:adjustViewBounds="true"
android:src="@drawable/navi_close" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/select_navi_top_title"
android:layout_marginRight="10dp"
android:background="@drawable/shape_select_navi_content" >
<TextView
android:id="@+id/select_navi_tip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择您常用的地图"
android:textColor="#999999"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:layout_below="@+id/select_navi_tip" >
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/select_baidu_navi_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/baidumap_navi" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="百度地图"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/select_baidu_navi_iv"
android:textSize="18sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageView
android:id="@+id/select_amap_navi_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/amap_navi"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="高德地图"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/select_amap_navi_iv"
android:textSize="18sp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
2.代码如下:
public class SelectMapNaviDialog extends AlertDialog implements
android.view.View.OnClickListener {
private View rootView;
private Context context;
private ImageView mapNavi_iv, aMapNavi_iv, close_iv;
private double tagLatitude, tagLongtitude;
private String tagAdres;
public SelectMapNaviDialog(Context context, double tagLatitude,
double tagLongtitude, String tagAdres) {
super(context);
this.context = context;
this.tagAdres = tagAdres;
this.tagLatitude = tagLatitude;
this.tagLongtitude = tagLongtitude;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().getAttributes().gravity = Gravity.CENTER;
this.getWindow().getAttributes().width = (BaseApplication.getContext()
.getWidthPixels() / 5) * 4;
this.setCanceledOnTouchOutside(false);
initView();
}
private void initView() {
rootView = View.inflate(context, R.layout.dialog_select_navi, null);
mapNavi_iv = (ImageView) rootView
.findViewById(R.id.select_baidu_navi_iv);
close_iv = (ImageView) rootView
.findViewById(R.id.select_navi_to_cacle_iv);
aMapNavi_iv = (ImageView) rootView
.findViewById(R.id.select_amap_navi_iv);
close_iv.setOnClickListener(this);
mapNavi_iv.setOnClickListener(this);
aMapNavi_iv.setOnClickListener(this);
this.setContentView(rootView);
}
protected void openBaiduNavi() {
LatLng startLatLng = null, endLatLng = null;
startLatLng = new LatLng(BaseApplication.getContext().latitude,
BaseApplication.getContext().longtitude);
endLatLng = new LatLng(tagLatitude, tagLongtitude);
RouteParaOption routeParaOption = new RouteParaOption();
routeParaOption.startPoint(startLatLng);
routeParaOption.endPoint(endLatLng);
try {
BaiduMapRoutePlan
.openBaiduMapDrivingRoute(routeParaOption, context);
} catch (BaiduMapAppNotSupportNaviException e) {
e.printStackTrace();
showToast("请安装百度地图");
}
}
public void showToast(String s){
MyUtils.showToast(s);
}
protected void openAmapNavi() {
// 先转换到高德地图的坐标
com.amap.api.maps.model.LatLng tagLatLng = converter(new com.amap.api.maps.model.LatLng(
tagLatitude, tagLongtitude));
com.amap.api.maps.model.LatLng startLatLng = converter(new com.amap.api.maps.model.LatLng(
BaseApplication.getContext().latitude,
BaseApplication.getContext().longtitude));
try {
RoutePara routePara = new RoutePara();
routePara.setStartPoint(startLatLng);
routePara.setEndPoint(tagLatLng);
routePara.setEndName(tagAdres);
routePara.setStartName(BaseApplication.getContext().address);
AMapUtils.openAMapDrivingRoute(routePara,
BaseApplication.getContext());
} catch (com.amap.api.maps.AMapException e) {
e.printStackTrace();
showToast("请安装高德地图");
/* // 如果没安装会进入异常,调起下载页面
// AMapUtils.getLatestAMapApp(getApplicationContext());
*/ } catch (Exception e) {
e.printStackTrace();
}
}
public com.amap.api.maps.model.LatLng converter(
com.amap.api.maps.model.LatLng sourceLatLng) {
CoordinateConverter converter = new CoordinateConverter(context);
converter.from(CoordType.BAIDU);
converter.coord(sourceLatLng);
return converter.convert();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.select_baidu_navi_iv:
openBaiduNavi();
this.dismiss();
break;
case R.id.select_amap_navi_iv:
openAmapNavi();
this.dismiss();
break;
case R.id.select_navi_to_cacle_iv:
this.dismiss();
break;
default:
break;
}
}
}
(五)解决百度地图和高德地图冲突问题:
问题:
Error generating final archive: Found duplicate file for APK: assets/lineDashTexture.png
原因:
导入的外包中含有两个相同lineDashTexture.png文件,导致冲突出错
解决方式:
用jar工具(WinRaR),打开后删掉lineDashTexture.png
(ps:因为本项目中高德地图只需要调用导航功能,所以就删掉高德地图jar中的重复文件)