1.成为开发者 http://lbsyun.baidu.com/
2.创建应用,申请密匙:
生成SHA1工具:http://download.csdn.net/detail/xiaoxin_android/9493330#comment
开发版:未签名
发布版:签名
3.把相关类库导入工程,SDK地址:http://lbsyun.baidu.com/sdk/download
4.添加相应权限
5.添加秘钥和服务
6.初始化SDK
7.创建带有Map的Activity
8.找到MapView控件,设置Map的生命周期,并获取BaiduMap
9.初始化定位设置并设置监听
10.检索的使用
11.检索的监听回调
11.设置mark点和点击事件
12.步行线路和测距
13.公交路线
14.自驾路线
15.路线的监听,及路线的文字描述
classMyOnGetRoutePlanResultListenerimplementsOnGetRoutePlanResultListener {
/**
* 步行结果
*
*@param
*/
@Override
public voidonGetWalkingRouteResult(WalkingRouteResult result) {
if(result ==null
|| SearchResult.ERRORNO.RESULT_NOT_FOUND== result.error) {
Toast.makeText(getApplicationContext(),"未查询到结果", Toast.LENGTH_SHORT).show();
return;
}
if(mMethod==BuXin) {
mWalkingRouteOverlay=newMyWalkingRouteOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(mWalkingRouteOverlay);
WalkingRouteLine line = result.getRouteLines().get(0);
mWalkingRouteOverlay.setData(line);// 设置数据
mWalkingRouteOverlay.addToMap();// 添加到地图上
mWalkingRouteOverlay.zoomToSpan();// 自动缩放级别
List allStep = line.getAllStep();
String content ="";
for(inti =0; i < allStep.size(); i++) {
WalkingRouteLine.WalkingStep walkingStep = allStep.get(i);
String instructions = walkingStep.getInstructions();
if(i < allStep.size()-1) {
content += instructions +"\n";
}else{
content += instructions;
}
}
mTvContent.clearComposingText();
mTvContent.setText(content);
mSl.setVisibility(View.VISIBLE);
setHeight();
}
}
/**
* 换乘路线结果回调
*
*@param
*/
@Override
public voidonGetTransitRouteResult(TransitRouteResult result) {
if(result ==null
|| SearchResult.ERRORNO.RESULT_NOT_FOUND== result.error) {
Toast.makeText(getApplicationContext(),"未查询到结果", Toast.LENGTH_SHORT).show();
return;
}
if(mMethod==GongJiao) {
mTransitRouteOverlay=newMyTransitRouteOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(mTransitRouteOverlay);
TransitRouteLine line = result.getRouteLines().get(0);
mTransitRouteOverlay.setData(line);// 设置数据
mTransitRouteOverlay.addToMap();// 添加到地图上
mTransitRouteOverlay.zoomToSpan();// 自动缩放级别
List allStep = line.getAllStep();
String content ="";
for(inti =0; i < allStep.size(); i++) {
TransitRouteLine.TransitStep transitStep = allStep.get(i);
String instructions = transitStep.getInstructions();
if(i
content += instructions +"\n";
}else{
content += instructions;
}
}
mTvContent.clearComposingText();
mTvContent.setText(content);
mSl.setVisibility(View.VISIBLE);
setHeight();
}
}
/**
* 跨城公共交通路线结果回调
*
*@parammassTransitRouteResult
*/
@Override
public voidonGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {
}
/**
* 驾车路线结果回调
*
*@paramresult
*/
@Override
public voidonGetDrivingRouteResult(DrivingRouteResult result) {
if(result ==null
|| SearchResult.ERRORNO.RESULT_NOT_FOUND== result.error) {
Toast.makeText(getApplicationContext(),"未查询到结果", Toast.LENGTH_SHORT).show();
return;
}
if(mMethod==ZiJia) {
mDrivingRouteOverlay=newMyDrivingRouteOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(mDrivingRouteOverlay);
DrivingRouteLine line = result.getRouteLines().get(0);
mDrivingRouteOverlay.setData(line);
mDrivingRouteOverlay.addToMap();
mDrivingRouteOverlay.zoomToSpan();
List routeLines = result.getRouteLines();
String content ="";
for(inti =0; i < routeLines.size(); i++) {
DrivingRouteLine drivingRouteLine = routeLines.get(i);
List allStep = drivingRouteLine.getAllStep();
for(intj =0; j < allStep.size(); j++) {
DrivingRouteLine.DrivingStep drivingStep = allStep.get(j);
String instructions = drivingStep.getInstructions();
if(j < allStep.size()-1) {
content += instructions +"\n";
}else{
content += instructions;
}
}
}
mTvContent.clearComposingText();
mTvContent.setText(content);
mSl.setVisibility(View.VISIBLE);
setHeight();
}
}
注意:6.0的写入权限需要动态申请,不然出不来地图
BaiDuMapAPI文档地址:http://developer.baidu.com/map/reference/
建议使用sdk里的api文档
github源码地址:https://github.com/LostDeer/BaiDuMap