Android 高德地图中路线规划绘制界面线路

下面代码是根据很多的经纬度的点,绘制出直线的线路。比较死板

    /*** 绘制景区的路线*/private void setRouteInfo(List<ScenicSpotGuideBean.ScenicLineListBean.CoordinateListBean> data) {List<LatLng> list = listToLatLng(data);mAllLinePolyline = mAmap.addPolyline((new PolylineOptions())//集合数据.addAll(list)//线的宽度.width(20).setDottedLine(false) // 关闭虚线.geodesic(true)//颜色.color(Color.argb(255, 36, 164, 255)));}

大概效果是这样的 ,是一个点到另一个点之间的直线绘制。

因为这个比较死板,在线路中绘制这个比较不好,然后就有了另一种绘制,就是线路的绘制。
先上效果图吧

因为界面要有这样的想过,所以直线绘制就不考虑了。
我这边大概就是根据高德地图中的线路搜索,搜索类型是数据驾车。进行异步搜索并且绘制。就可以出这样的效果。

 // 创建线路搜索对象RouteSearch mRouteSearch = null;// 对象进行初始化mRouteSearch = new RouteSearch(this);// 搜索监听mRouteSearch.setRouteSearchListener(mRouteSearchListener);

由于我这边是绘制多条线路,我这边是这样做的。

private void SurfacePolyDaysLine() {// 清除界面上之前绘制的线路clearAllLineMarker();// 所有线路需要将开始的线路经纬度放到这个集合中List<LatLng> list = new ArrayList<>();for (int j = 0; j < list.size() - 1; j++) { // 遍历当前集合进行搜索线路// 传入当前线路的起点和终点坐标,然后进行绘制searchRouteResult(new LatLonPoint(list.get(j).latitude, list.get(j).longitude), new LatLonPoint(list.get(j + 1).latitude, list.get(j + 1).longitude));}// update map zoomsetMapBounds(list);}

下面是搜索线路代码

    /*** start search line*/public void searchRouteResult(LatLonPoint mStartPoint, LatLonPoint mEndPoint) {// 传入起点坐标、终点坐标final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(mStartPoint, mEndPoint);// DriveRouteQuery 类型是驾车的路线搜索  DRIVING_SINGLE_SHORTEST 类型表示返回单个路线最短的数据集RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, RouteSearch.DRIVING_SINGLE_SHORTEST, null, null, "");// 开始异步搜索处理mRouteSearch.calculateDriveRouteAsyn(query);}

搜索监听事件 ,将监听到的线路进行绘制在界面上就可以了。

 /*** search line listener*/RouteSearch.OnRouteSearchListener mRouteSearchListener = new RouteSearch.OnRouteSearchListener() {@Overridepublic void onBusRouteSearched(BusRouteResult busRouteResult, int i) {// bus}@Overridepublic void onDriveRouteSearched(DriveRouteResult result, int errorCode) {// drivingif (errorCode == AMapException.CODE_AMAP_SUCCESS) {if (result != null && result.getPaths() != null) {if (result.getPaths().size() > 0) {DriveRouteResult mDriveRouteResult = result;final DrivePath drivePath = mDriveRouteResult.getPaths().get(0);if (drivePath == null) { // 表示返回的线路是空的情况,就不管return;}// 自定义的一个绘制处理SharedHomeDrivingRouteOverlay drivingRouteOverlay = new SharedHomeDrivingRouteOverlay(SharedNavigationMapActivity.this, mAmap, drivePath,mDriveRouteResult.getStartPos(),mDriveRouteResult.getTargetPos(), null);drivingRouteOverlay.setNodeIconVisibility(false); // 是否显示节点图标,这个里面没有设置所以没有,也不需要显示drivingRouteOverlay.setIsColorfulline(false); // 是否显示线路的交通状态drivingRouteOverlay.removeFromMap(); // 先进行移除掉drivingRouteOverlay.addToMap(); // 添加到地图上面
//                        drivingRouteOverlay.zoomToSpan(); // 根据线路的起点和终点自动缩放zoom值,显示在屏幕的中间} else if (result != null && result.getPaths() == null) {}} else {}} else {}}@Overridepublic void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {// walk}@Overridepublic void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {// ride}};

自定义绘制线路界面,是根据高德地图《3D地图示例》中的出行路线规划中的驾车路径规划更改所得的,如果想要了解更多可以去参考这个demo文件的代码。


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;import com.amap.api.maps.AMap;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveStep;
import com.amap.api.services.route.TMC;
import cn.xmzt.www.R;import java.util.ArrayList;
import java.util.List;/*** 导航路线图层类。*/
public class SharedHomeDrivingRouteOverlay extends RouteOverlay{private DrivePath drivePath;private List<LatLonPoint> throughPointList;private List<Marker> throughPointMarkerList = new ArrayList<Marker>();private boolean throughPointMarkerVisible = true;private List<TMC> tmcs;private PolylineOptions mPolylineOptions;private PolylineOptions mPolylineOptionscolor;private Context mContext;private boolean isColorfulline = true;private float mWidth = 90;private List<LatLng> mLatLngsOfPath;public void setIsColorfulline(boolean iscolorfulline) {this.isColorfulline = iscolorfulline;}/*** 根据给定的参数,构造一个导航路线图层类对象。** @param amap      地图对象。* @param path 导航路线规划方案。* @param context   当前的activity对象。*/public SharedHomeDrivingRouteOverlay(Context context, AMap amap, DrivePath path,LatLonPoint start, LatLonPoint end, List<LatLonPoint> throughPointList) {super(context);mContext = context; mAMap = amap; this.drivePath = path;startPoint = AMapUtil.convertToLatLng(start);endPoint = AMapUtil.convertToLatLng(end);this.throughPointList = throughPointList;}public float getRouteWidth() {return mWidth;}/*** 设置路线宽度** @param mWidth 路线宽度,取值范围:大于0*/public void setRouteWidth(float mWidth) {this.mWidth = mWidth;}/*** 添加驾车路线添加到地图上显示。*/public void addToMap() {initPolylineOptions();try {if (mAMap == null) {return;}if (mWidth == 0 || drivePath == null) {return;}mLatLngsOfPath = new ArrayList<LatLng>();tmcs = new ArrayList<TMC>();List<DriveStep> drivePaths = drivePath.getSteps();for (DriveStep step : drivePaths) {List<LatLonPoint> latlonPoints = step.getPolyline();List<TMC> tmclist = step.getTMCs();tmcs.addAll(tmclist);addDrivingStationMarkers(step, convertToLatLng(latlonPoints.get(0)));for (LatLonPoint latlonpoint : latlonPoints) {mPolylineOptions.add(convertToLatLng(latlonpoint));mLatLngsOfPath.add(convertToLatLng(latlonpoint));}}if (startMarker != null) {startMarker.remove();startMarker = null;}if (endMarker != null) {endMarker.remove();endMarker = null;}addThroughPointMarker();if (isColorfulline && tmcs.size()>0 ) {//              colorWayUpdate(tmcs);colorWayUpdates(tmcs);showcolorPolyline();}else {showPolyline();}            } catch (Throwable e) {e.printStackTrace();}}/*** 初始化线段属性*/private void initPolylineOptions() {mPolylineOptions = null;mPolylineOptions = new PolylineOptions();mPolylineOptions.color(getDriveColor()).width(getRouteWidth());Bitmap bitmap = (Bitmap) BitmapFactory.decodeResource(mContext.getResources(),R.drawable.map_line_info_green);mPolylineOptions.setCustomTexture(BitmapDescriptorFactory.fromBitmap(bitmap));// TODO 如果要添加图片的话 需要将setIsColorfulline 设置为true才会显示mPolylineOptions.width(getRouteWidth());mPolylineOptions.setUseTexture(true);}private void showPolyline() {addPolyLine(mPolylineOptions);}private void showcolorPolyline() {addPolyLine(mPolylineOptionscolor);}/*** 根据不同的路段拥堵情况展示不同的颜色** @param tmcSection*/private void colorWayUpdate(List<TMC> tmcSection) {if (mAMap == null) {return;}if (tmcSection == null || tmcSection.size() <= 0) {return;}TMC segmentTrafficStatus;mPolylineOptionscolor = null;mPolylineOptionscolor = new PolylineOptions();Bitmap bitmap = (Bitmap) BitmapFactory.decodeResource(mContext.getResources(),R.drawable.map_line_info_green);mPolylineOptionscolor.setCustomTexture(BitmapDescriptorFactory.fromBitmap(bitmap));// TODO 如果要添加图片的话 需要将setIsColorfulline 设置为true才会显示mPolylineOptionscolor.width(getRouteWidth());mPolylineOptionscolor.setUseTexture(true);List<Integer> colorList = new ArrayList<Integer>();mPolylineOptionscolor.add(AMapUtil.convertToLatLng(tmcSection.get(0).getPolyline().get(0)));colorList.add(getDriveColor());for (int i = 0; i < tmcSection.size(); i++) {segmentTrafficStatus = tmcSection.get(i);int color = getcolor(segmentTrafficStatus.getStatus());List<LatLonPoint> mployline = segmentTrafficStatus.getPolyline();for (int j = 1; j < mployline.size(); j++) {mPolylineOptionscolor.add(AMapUtil.convertToLatLng(mployline.get(j)));colorList.add(color);}}colorList.add(getDriveColor());mPolylineOptionscolor.colorValues(colorList);}/*** 根据不同的路段拥堵情况展示不同的颜色的图片** @param tmcSection*/private void colorWayUpdates(List<TMC> tmcSection) {if (mAMap == null) {return;}if (tmcSection == null || tmcSection.size() <= 0) {return;}TMC segmentTrafficStatus;mPolylineOptionscolor = null;mPolylineOptionscolor = new PolylineOptions();
//        Bitmap bitmap = (Bitmap) BitmapFactory.decodeResource(mContext.getResources(),R.drawable.custtexture_green);
//        mPolylineOptionscolor.setCustomTexture(BitmapDescriptorFactory.fromBitmap(bitmap));// TODO 如果要添加图片的话 需要将setIsColorfulline 设置为true才会显示
//        mPolylineOptionscolor.width(getRouteWidth());
//        mPolylineOptionscolor.setUseTexture(true);List<Integer> colorList = new ArrayList<Integer>();List<BitmapDescriptor> list = new ArrayList<>();int position = 0;mPolylineOptionscolor.add(AMapUtil.convertToLatLng(tmcSection.get(0).getPolyline().get(0)));list.add(BitmapDescriptorFactory.fromResource(getDriveColorRes()));colorList.add(position);position += 1;for (int i = 0; i < tmcSection.size(); i++) {segmentTrafficStatus = tmcSection.get(i);int colorres = getcolorRes(segmentTrafficStatus.getStatus());List<LatLonPoint> mployline = segmentTrafficStatus.getPolyline();for (int j = 1; j < mployline.size(); j++) {mPolylineOptionscolor.add(AMapUtil.convertToLatLng(mployline.get(j)));list.add(BitmapDescriptorFactory.fromResource(colorres));colorList.add(position);position += 1;}}list.add(BitmapDescriptorFactory.fromResource(getDriveColorRes()));colorList.add(position);mPolylineOptionscolor.setCustomTextureList(list);mPolylineOptionscolor.setCustomTextureIndex(colorList);mPolylineOptionscolor.width(getRouteWidth());mPolylineOptionscolor.setUseTexture(true);}private int getcolor(String status) {if (status.equals("畅通")) { // 畅通情况下 绿色显示return Color.parseColor("#11BA1F");} else if (status.equals("缓行")) { // 缓行情况下 黄色显示return Color.parseColor("#FFAD37");} else if (status.equals("拥堵")) { // 拥堵情况下 红色显示return Color.parseColor("#F41A25");} else if (status.equals("严重拥堵")) { // 严重拥堵 猪肝红显示return Color.parseColor("#AB1717");} else { // 未知情况下蓝色显示return Color.parseColor("#1590FF");}  }private int getcolorRes(String status) {if (status.equals("畅通")) { // 畅通情况下 绿色显示return R.drawable.map_line_info_green;} else if (status.equals("缓行")) { // 缓行情况下 黄色显示return R.drawable.map_line_info_yellow;} else if (status.equals("拥堵")) { // 拥堵情况下 红色显示return R.drawable.map_line_info_red;} else if (status.equals("严重拥堵")) {  // 严重拥堵 猪肝红显示return R.drawable.map_line_info_red_more;} else { // 未知情况下蓝色显示return R.drawable.map_line_no_info_blue;}}protected int getDriveColorRes() { // 畅通情况下 绿色显示 TODO 可能会换成蓝色return R.drawable.map_line_info_green;}public LatLng convertToLatLng(LatLonPoint point) {return new LatLng(point.getLatitude(),point.getLongitude());}/*** @param driveStep* @param latLng*/private void addDrivingStationMarkers(DriveStep driveStep, LatLng latLng) {addStationMarker(new MarkerOptions().position(latLng).title("\u65B9\u5411:" + driveStep.getAction()+ "\n\u9053\u8DEF:" + driveStep.getRoad()).snippet(driveStep.getInstruction()).visible(nodeIconVisible).anchor(0.5f, 0.5f).icon(getDriveBitmapDescriptor()));}@Overrideprotected LatLngBounds getLatLngBounds() {LatLngBounds.Builder b = LatLngBounds.builder();b.include(new LatLng(startPoint.latitude, startPoint.longitude));b.include(new LatLng(endPoint.latitude, endPoint.longitude));if (this.throughPointList != null && this.throughPointList.size() > 0) {for (int i = 0; i < this.throughPointList.size(); i++) {b.include(new LatLng(this.throughPointList.get(i).getLatitude(),this.throughPointList.get(i).getLongitude()));}}return b.build();}public void setThroughPointIconVisibility(boolean visible) {try {throughPointMarkerVisible = visible;if (this.throughPointMarkerList != null&& this.throughPointMarkerList.size() > 0) {for (int i = 0; i < this.throughPointMarkerList.size(); i++) {this.throughPointMarkerList.get(i).setVisible(visible);}}} catch (Throwable e) {e.printStackTrace();}}private void addThroughPointMarker() {if (this.throughPointList != null && this.throughPointList.size() > 0) {LatLonPoint latLonPoint = null;for (int i = 0; i < this.throughPointList.size(); i++) {latLonPoint = this.throughPointList.get(i);if (latLonPoint != null) {throughPointMarkerList.add(mAMap.addMarker((new MarkerOptions()).position(new LatLng(latLonPoint.getLatitude(), latLonPoint.getLongitude())).visible(throughPointMarkerVisible).icon(getThroughPointBitDes()).title("\u9014\u7ECF\u70B9")));}}}}private BitmapDescriptor getThroughPointBitDes() {return BitmapDescriptorFactory.fromResource(R.drawable.amap_through);}/*** 获取两点间距离** @param start* @param end* @return*/public static int calculateDistance(LatLng start, LatLng end) {double x1 = start.longitude;double y1 = start.latitude;double x2 = end.longitude;double y2 = end.latitude;return calculateDistance(x1, y1, x2, y2);}public static int calculateDistance(double x1, double y1, double x2, double y2) {final double NF_pi = 0.01745329251994329; // 弧度 PI/180x1 *= NF_pi;y1 *= NF_pi;x2 *= NF_pi;y2 *= NF_pi;double sinx1 = Math.sin(x1);double siny1 = Math.sin(y1);double cosx1 = Math.cos(x1);double cosy1 = Math.cos(y1);double sinx2 = Math.sin(x2);double siny2 = Math.sin(y2);double cosx2 = Math.cos(x2);double cosy2 = Math.cos(y2);double[] v1 = new double[3];v1[0] = cosy1 * cosx1 - cosy2 * cosx2;v1[1] = cosy1 * sinx1 - cosy2 * sinx2;v1[2] = siny1 - siny2;double dist = Math.sqrt(v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2]);return (int) (Math.asin(dist / 2) * 12742001.5798544);}//获取指定两点之间固定距离点public static LatLng getPointForDis(LatLng sPt, LatLng ePt, double dis) {double lSegLength = calculateDistance(sPt, ePt);double preResult = dis / lSegLength;return new LatLng((ePt.latitude - sPt.latitude) * preResult + sPt.latitude, (ePt.longitude - sPt.longitude) * preResult + sPt.longitude);}/*** 去掉DriveLineOverlay上的线段和标记。*/@Overridepublic void removeFromMap() {try {super.removeFromMap();if (this.throughPointMarkerList != null&& this.throughPointMarkerList.size() > 0) {for (int i = 0; i < this.throughPointMarkerList.size(); i++) {this.throughPointMarkerList.get(i).remove();}this.throughPointMarkerList.clear();}} catch (Throwable e) {e.printStackTrace();}}
}

高德地图小知识,共勉

Android 高德地图中路线规划绘制界面线路相关推荐

  1. Android高德地图多路线规划----可点击选择路线

    最近项目上有用到高德sdk,需求上要求多路线规划,并且可以在地图上点击选择路线.本着不想重复造轮子的驱使下(其实想偷懒),网上似乎没有例子,也可能是我找不到.最后没办法,只能自己实现. 在此做为记录, ...

  2. android 步行距离,Android 高德地图 步行 路线规划

    路线规划就是给RouteSearch设置一个监听,里面有步行规划,骑车,驾车,公交等线路的规划 第 1 步,初始化 RouteSearch 对象 routeSearch = new RouteSear ...

  3. Android 高德地图 步行 路线规划

    路线规划就是给RouteSearch设置一个监听,里面有步行规划,骑车,驾车,公交等线路的规划 第 1 步,初始化 RouteSearch 对象 routeSearch = new RouteSear ...

  4. (转载)Android高德地图多路线规划----可点击选择路线

    (转载)https://blog.csdn.net/VRoymond/article/details/79898400

  5. vue人员轨迹_Vue项目(vuecli3.0搭建)集成高德地图实现路线轨迹绘制

    先看最后实现的效果图 高德地图api文档 使用 1.在index.html里面引入高德地图js文件 2.引入之后我们就直接可以在vue组件里面使用了 创建initMap方法,在mounted钩子函数中 ...

  6. uniapp H5端使用高德地图完成路线规划

    本项目是H5端,APP端地图的使用方法请参考我的另一篇博客uniapp,map地图组件打包原生APP 首先到高德地图API,申请web端key 参考高德H5端教程开始写代码高德地图JS API 1.准 ...

  7. 高德地图实现路线规划+轨迹回放(显示车牌)

    联系作者Q/V:783021975 Tips: 1.高德好的方法非同步执行.一定要注意执行顺序或等待执行完成再进行具体的操作 2.高德地图不支持label旋转哦~ 3.百度是支持的.可以参考 百度地图 ...

  8. 高德地图API路线规划

    高德地图API基础使用 高德地图API体验 申请Key 初体验 添加插件 点击事件返回地址值 两点之间获取驾车导航路线 高德地图API体验 申请Key 申请高德地图账号 登录后,应用管理下创建自己的应 ...

  9. 高德地图之路线规划 多点路线规划路线最短原则之二 TSP算法

    经过这段时间的使用和研究,我发现点和点之间的最短路线并不是最优的.针对所有点的组合可能, 得到的最短路线才是最好的(也就是TSP方法),然后再结合点对点之间的距离进行比较,得到的才是相对最优方案. 举 ...

最新文章

  1. Python Matplotlib 中文显示参数设置
  2. 基础设备----笔记
  3. 【ZOJ - 3329】One Person Game(带循环的概率dp,数学期望,高斯消元,数学)
  4. 【2016年第5期】基于深度学习的光学遥感机场与飞行器目标识别技术
  5. ubuntu 16.04 nfs服务的搭建
  6. python入门三:文件操作
  7. Owin的URL编码怎么搞?以前都是HttpUtility.UrlEncode之类的,现在连system.web都没了,肿么办?...
  8. eclipse开发SVN下文件显示修改时间和提交作者的方法
  9. 按键消抖的原理与解决方案浅论
  10. 7.09—057—周二
  11. php revel,Revel命令行工具源码阅读
  12. 云服务器上硬盘是虚拟的吗,虚拟硬盘映射在主机上使用
  13. one-hot向量形式
  14. 欧阳的科研历程-1 目标
  15. GAN GAN Inversion
  16. 《水经注地图服务》缓存管理说明
  17. ERD Online 元数据管理开放平台
  18. fabric框架学习
  19. pandas中diff用法
  20. 《比尔总动员》1——20级变强攻略之胶囊制作

热门文章

  1. 【文献阅读】ChangeNet——变化检测网络(A. Varghese等人,ECCV,2018)
  2. 在html中加入pdf文件吗,如何在网页中显示PDF文件
  3. python中description_python中cursor.description什么意思
  4. 计算机技术发展作文,【推荐】科技发展作文三篇
  5. canvas绘图在高清屏显示模糊
  6. 微服务实战之春云与刀客(一)—— 微服务开篇
  7. 阿里云部署k8s集群
  8. 伤感日志:眼泪是什么颜色,什么味道
  9. 管理学书籍推荐:这10本优秀的管理类书籍最值得一读
  10. ubuntu 制作本地源 离线安装