您的位置:首页 > 移动开发 > Android开发

高德地图 android 开发 路线规划 自定义 修改线路颜色和宽度

2016-08-20 16:04 711 查看
public class MyDrivingRouteOverlay extends DrivingRouteOverlay {
public int color;// 路线颜色
public float lineWidth;// 路线宽度

public MyDrivingRouteOverlay(Context context, AMap amap, DrivePath drivepath, LatLonPoint latlonpoint,
LatLonPoint latlonpoint1) {
super(context, amap, drivepath, latlonpoint, latlonpoint1);
}

public MyDrivingRouteOverlay(Context context, AMap amap, DrivePath drivepath, LatLonPoint latlonpoint,
LatLonPoint latlonpoint1, List<LatLonPoint> list) {
super(context, amap, drivepath, latlonpoint, latlonpoint1, list);
}

/* 一个工具方法,修改颜色和宽度 */
public void setView(int color, float width) {
this.color = color;
lineWidth = width;
}

@SuppressWarnings("static-access")
@Override
protected void addStartAndEndMarker() {

BitmapDescriptorFactory bdf = new BitmapDescriptorFactory();
BitmapDescriptor startbd = bdf.fromResource(R.drawable.factory);
BitmapDescriptor endbd = bdf.fromResource(R.drawable.project);
startMarker = mAMap.addMarker((new MarkerOptions()).position(startPoint).icon(startbd).title("\u8D77\u70B9"));
endMarker = mAMap.addMarker((new MarkerOptions()).position(endPoint).icon(endbd).title("\u7EC8\u70B9"));
}

public void setMarkerIcon() {
Marker startmarker = this.startMarker;
startmarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.factory));
Marker endmarker = this.endMarker;
endmarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.project));
}

@Override
protected float getRouteWidth() {
// TODO Auto-generated method stub
return lineWidth;
}

@Override
protected int getDriveColor() {
// TODO Auto-generated method stub
return color;
}
}

在自定义的监听器里面用 

public class GDRouteSearchListener implements OnRouteSearchListener {

private Context mContext;
private AMap mAMap;
private BusRouteResult mBusRouteResult;
private DriveRouteResult mDriveRouteResult;
private int direction;

//direction 是我个人用来标注去程返程的一个参数  用来判断 规划的线路是去程还是返程 
public GDRouteSearchListener(Context context, AMap aMap, int direction) {
this.mContext = context;
this.mAMap = aMap;
this.direction = direction;
}

public GDRouteSearchListener(Context context, AMap aMap) {
this.mContext = context;
this.mAMap = aMap;

}

/**
* 公交换乘路径规划结果的回调方法。
*/
@Override
public void onBusRouteSearched(BusRouteResult result, int errorCode) {

}

/**
* 驾车路径规划结果的回调方法。
*/
@Override
public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {

if (1000 == errorCode) {
if (null != result) {
if (null != result.getPaths()) {
if (result.getPaths().size() > 0) {
mDriveRouteResult = result;
DrivePath drivePath = mDriveRouteResult.getPaths().get(0);
;
if (direction == 1 || direction == 0) {
MyDrivingRouteOverlay myDrivingRouteOverlay = new MyDrivingRouteOverlay(mContext, mAMap,
drivePath, mDriveRouteResult.getStartPos(), mDriveRouteResult.getTargetPos());
myDrivingRouteOverlay.setView(0xff0c91f8, 10);
myDrivingRouteOverlay.setNodeIconVisibility(false);// 路段节点图标控制显示接口。
myDrivingRouteOverlay.removeFromMap();// 去掉DriveLineOverlay上所有的Marker。
myDrivingRouteOverlay.addToMap();// 绘制节点和线路
myDrivingRouteOverlay.zoomToSpan();

} else {

MyDrivingRouteOverlaytwo myDrivingRouteOverlay = new MyDrivingRouteOverlaytwo(mContext,
mAMap, drivePath, mDriveRouteResult.getStartPos(),
mDriveRouteResult.getTargetPos());
myDrivingRouteOverlay.setView(0xff33cc60, 10);
myDrivingRouteOverlay.setNodeIconVisibility(false);
myDrivingRouteOverlay.removeFromMap();
myDrivingRouteOverlay.addToMap();
myDrivingRouteOverlay.zoomToSpan();

}

// DrivingRouteOverlay mDrivingRouteOverlay = new
// DrivingRouteOverlay(mContext, mAMap, drivePath,
// mDriveRouteResult.getStartPos(),
// mDriveRouteResult.getTargetPos());
// // 路段节点图标控制显示接口。
// mDrivingRouteOverlay.setNodeIconVisibility(false);
// mDrivingRouteOverlay.removeFromMap();
// mDrivingRouteOverlay.addToMap();
// mDrivingRouteOverlay.zoomToSpan();
}
}
}
}
}

/**
* 步行路径规划结果的回调方法。
*/
@Override
public void onWalkRouteSearched(WalkRouteResult arg0, int arg1) {

}

}

在主函数里面掉用

private RouteSearch mRouteSearch;
private GDRouteSearchListener mGdRouteSearchListener;

mRouteSearch = new RouteSearch(this);
mGdRouteSearchListener = new GDRouteSearchListener(CarMapActivity.this, amp, direction);
mRouteSearch.setRouteSearchListener(mGdRouteSearchListener);

// 第一个参数表示路径规划的起点和终点,第二个参数表示驾车模式,第三个参数表示途经点,第四个参数表示避让区域,第五个参数表示避让道路
DriveRouteQuery query = new DriveRouteQuery(fromAndTo, RouteSearch.DrivingDefault, null, null, "");
mRouteSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 高德地图