安卓开发地图路径规划
注意:高德地图的overlay类需要自己去引入
一、步行
RouteSearch.WalkRouteQuery query2 = new RouteSearch.WalkRouteQuery(fromAndTo);
routeSearch.calculateWalkRouteAsyn(query2);
@Override
public void onWalkRouteSearched(WalkRouteResult result, int errorCode) {
//步行路线规划
aMap.clear();// 清理地图上的所有覆盖物
if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getPaths() != null) {
if (result.getPaths().size() > 0) {
mWalkRouteResult = result;
final WalkPath walkPath = mWalkRouteResult.getPaths()
.get(0);
WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(this, aMap, walkPath, mWalkRouteResult.getStartPos(), mWalkRouteResult.getTargetPos());
//计算所有点经纬度
List<WalkStep> walkPaths = walkPath.getSteps();
List<LatLng> pass_latlng;
pass_latlng=new ArrayList<LatLng>();
pass_latlng.add(AMapServicesUtil.convertToLatLng(mWalkRouteResult.getStartPos()));
for (int i = 0; i < walkPaths.size(); i++) {
WalkStep walkStep = walkPaths.get(i);
LatLng latLng = AMapServicesUtil.convertToLatLng(walkStep
.getPolyline().get(0));
pass_latlng.add(latLng);
}
pass_latlng.add(AMapServicesUtil.convertToLatLng(mWalkRouteResult.getTargetPos()));
walkRouteOverlay.removeFromMap();
//分析源码,找到addToMap()方法可以获得路径详情
walk_details= walkRouteOverlay.addToMap();
System.out.println(walk_details.toString());
walkRouteOverlay.zoomToSpan(pass_latlng);
walkRouteOverlay.setNodeIconVisibility(false);
int dis = (int) walkPath.getDistance();
long dur = (int) walkPath.getDuration();
int round = Math.round(dis);
String gl = Tools.mi_change_kil(dis);
String ti = Tools.get_h_m_s(dur);
String dur2 = AMapUtil.getFriendlyTime((int) walkPath.getDuration());
String dis2 = AMapUtil
.getFriendlyLength((int) walkPath.getDistance());
System.out.println("距离目的地约"+ gl+"步行预计"+ ti +"后到达");
route_time_distance.setText(dur2+" "+dis2);
get_walk_route_details.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, WalkRouteDetailActivity.class);
intent.putExtra("walk_path", walkPath);
intent.putExtra("walk_result",
mWalkRouteResult);
startActivity(intent);
}
});
mWalkRouteResult.getWalkQuery();
} else if (result != null && result.getPaths() == null) {
// ToastUtil.show(mContext, R.string.no_result);
System.out.println("错误");
}
} else {
// ToastUtil.show(mContext, R.string.no_result);
System.out.println("错误");
}
} else {
// ToastUtil.showerror(this.getApplicationContext(), errorCode);
System.out.println("错误");
}
}
二、驾车
RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, DRIVING_SINGLE_DEFAULT, null, null, "");
routeSearch.calculateDriveRouteAsyn(query);
@Override
public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int errorCode) {
aMap.clear();// 清理地图上的所有覆盖物
if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
if (driveRouteResult != null && driveRouteResult.getPaths() != null) {
if (driveRouteResult.getPaths().size() > 0) {
mDriveRouteResult = driveRouteResult;
final DrivePath drivePath = mDriveRouteResult.getPaths()
.get(0);
if(drivePath == null) {
return;
}
DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
this, aMap, drivePath,
mDriveRouteResult.getStartPos(),
mDriveRouteResult.getTargetPos(), null);
//计算所有点经纬度
List<DriveStep> drivePaths = drivePath.getSteps();
List<LatLng> pass_latlng;
pass_latlng=new ArrayList<LatLng>();
pass_latlng.add(AMapServicesUtil.convertToLatLng(mDriveRouteResult.getStartPos()));
for (int i = 0; i < drivePaths.size(); i++) {
DriveStep driveStep = drivePaths.get(i);
LatLng latLng = AMapServicesUtil.convertToLatLng(driveStep
.getPolyline().get(0));
pass_latlng.add(latLng);
}
pass_latlng.add(AMapServicesUtil.convertToLatLng(mDriveRouteResult.getTargetPos()));
drivingRouteOverlay.setNodeIconVisibility(false);//设置节点marker是否显示
drivingRouteOverlay.setIsColorfulline(true);//是否用颜色展示交通拥堵情况,默认true
drivingRouteOverlay.removeFromMap();
drivingRouteOverlay.addToMap();
drivingRouteOverlay.zoomToSpan(pass_latlng);
// mBottomLayout.setVisibility(View.VISIBLE);
int dis = (int) drivePath.getDistance();
int dur = (int) drivePath.getDuration();
String des = AMapUtil.getFriendlyTime(dur)+"("+AMapUtil.getFriendlyLength(dis)+")";
route_time_distance.setText(AMapUtil.getFriendlyTime(dur)+" "+AMapUtil.getFriendlyLength(dis));
get_walk_route_details.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext,
DriveRouteDetailActivity.class);
intent.putExtra("drive_path", drivePath);
intent.putExtra("drive_result",
mDriveRouteResult);
startActivity(intent);
}
});
} else if (driveRouteResult != null && driveRouteResult.getPaths() == null) {
System.out.println("error");
}
} else {
System.out.println("error"); }
} else {
System.out.println("error");
}
}
三、骑行
RouteSearch.RideRouteQuery query3 = new RouteSearch.RideRouteQuery(fromAndTo);
routeSearch.calculateRideRouteAsyn(query3);
@Override
public void onRideRouteSearched(RideRouteResult result, int errorCode) {
aMap.clear();// 清理地图上的所有覆盖物
if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getPaths() != null) {
if (result.getPaths().size() > 0) {
mRideRouteResult = result;
final RidePath ridePath = mRideRouteResult.getPaths()
.get(0);
if(ridePath == null) {
return;
}
RideRouteOverlay rideRouteOverlay = new RideRouteOverlay(
this, aMap, ridePath,
mRideRouteResult.getStartPos(),
mRideRouteResult.getTargetPos());
//计算所有点经纬度
List<RideStep> ridePaths = ridePath.getSteps();
List<LatLng> pass_latlng;
pass_latlng=new ArrayList<LatLng>();
pass_latlng.add(AMapServicesUtil.convertToLatLng(mRideRouteResult.getStartPos()));
for (int i = 0; i < ridePaths.size(); i++) {
RideStep rideStep = ridePaths.get(i);
LatLng latLng = AMapServicesUtil.convertToLatLng(rideStep
.getPolyline().get(0));
pass_latlng.add(latLng);
}
pass_latlng.add(AMapServicesUtil.convertToLatLng(mRideRouteResult.getTargetPos()));
rideRouteOverlay.removeFromMap();
rideRouteOverlay.addToMap();
rideRouteOverlay.zoomToSpan(pass_latlng);
// mBottomLayout.setVisibility(View.VISIBLE);
int dis = (int) ridePath.getDistance();
int dur = (int) ridePath.getDuration();
String des = AMapUtil.getFriendlyTime(dur)+"("+AMapUtil.getFriendlyLength(dis)+")";
route_time_distance.setText(AMapUtil.getFriendlyTime(dur)+" "+AMapUtil.getFriendlyLength(dis));
get_walk_route_details.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext,
RideRouteDetailActivity.class);
intent.putExtra("ride_path", ridePath);
intent.putExtra("ride_result",
mRideRouteResult);
startActivity(intent);
}
});
} else if (result != null && result.getPaths() == null) {
}
} else {
}
} else {
}
}
四、公交
RouteSearch.BusRouteQuery query1 = new RouteSearch.BusRouteQuery(fromAndTo, RouteSearch.BusLeaseWalk, "", 1);
routeSearch.calculateBusRouteAsyn(query1);
@Override
public void onBusRouteSearched(BusRouteResult result, int errorCode) {
aMap.clear();// 清理地图上的所有覆盖物
if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getPaths() != null) {
if (result.getPaths().size() > 0) {
mBusResultList.setVisibility(View.VISIBLE);
mBusRouteResult = result;
BusResultListAdapter mBusResultListAdapter = new BusResultListAdapter(mContext, mBusRouteResult);
mBusResultList.setAdapter(mBusResultListAdapter);
} else if (result != null && result.getPaths() == null) {
System.out.println("失败");
}
} else {
System.out.println("失败");
}
} else {
System.out.println("失败");
}
}
效果图