下面代码是根据很多的经纬度的点,绘制出直线的线路。比较死板
/**
* 绘制景区的路线
*/
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 zoom
setMapBounds(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() {
@Override
public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {
// bus
}
@Override
public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {
// driving
if (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 {
}
}
@Override
public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
// walk
}
@Override
public 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()));
}
@Override
protected 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/180
x1 *= 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上的线段和标记。
*/
@Override
public 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();
}
}
}
高德地图小知识,共勉