繼續上一篇 寫路線規劃
上一篇寫道多點路線規劃 高德地圖僅僅只會按照你給出的點的順序進行路線規劃 並不會智能的給你按照最近或者最快進行規劃 舉個例子 A B C D 四個點路線規劃 A是起點 D是終點 B C 是途徑點 加入你按照 A - B - C - D 的順序給高德 那麼高德返回給你的就是 A - B - C - D這個順序 加入這時A - B - C - D 並不是最優的路線 是不是就萌幣了
不要着急 下面我就是給你一個最優路線
說一下思路 以A - B - C - D 爲例
首先起點A 終點 D 不會變 我們不用管
現在命名:
AStartLat 起點緯度 ASP A的起點
AStartLon 起點經度
BStartLat B起點緯度 BSP
BStartLon B起點經度
CStartLat C起點緯度 CSP
CStartLon C起點經度
BEndLat B終點緯度 BEP
BEndLon B終點經度
CEndLat B終點緯度 CEP
CEndLon B終點經度
簡單做個用例圖
A —> B------>C----->D 原順序
A----->B 10KM
A----->C 3KM
A---->C---->B---->D 排序後順序
我們要做的就是 A —> B------>C----->D 計算出每兩個點的距離 然後比較大小 得到距離最近的點 並以這個點爲起點 再次計算下一輪的最近的點 最後得到一個最短的路線 就是我們的最後的目的
好 下面上代碼
> // 排序所有途經點
> private List<Point> sortPoint(List<Line> sortList) {
> for (int no = 0; no < sortList.size()*2-1; no++) {
> if (no!=0){// 不是第一次 要判斷添加終點
> for (int i = 1; i < startList.size(); i++) {
> for (int j = 1; j < sortList.size(); j++) {
> if (startList.get(i).getLlPoint()==sortList.get(j).getSrartPoint()&&startList.get(i).getLlPoint()!=sortList.get(j).getEndPoint()){
> Point p = new Point();
> p.setLlPoint(sortList.get(j).getEndPoint());
> p.setDistance(0);
> startList.add(p);
> }
> }
> }
> }else {// 第一次 只添加所有起點
> for (int i = 0; i < sortList.size(); i++) {
> Point p = new Point();
> p.setLlPoint(sortList.get(i).getSrartPoint());
> p.setDistance(0);
> startList.add(p);
> }
> }
> for (int i = 0; i < startList.size()-1; i++) {
> for (int j = (i+1); j < startList.size(); j++) {
> if (i == 0){
> startList.get(j).setDistance(DrivingRouteOverlay.calculateDistance(convertToLatLng(startList.get(i).getLlPoint()),convertToLatLng(startList.get(j).getLlPoint())));
> }
> }
> }
> Collections.sort(startList, new Comparator<Point>() {
> @Override
> public int compare(Point o1, Point o2) {
> if (o1.getDistance()==o2.getDistance()){
> return o1.getDistance();
> }else {
> return o1.getDistance() - o2.getDistance();
> }
> }
> });
> for (int i = 0; i < startList.size(); i++) {
> for (int j = 0; j < startList.size(); j++) {
> if (i!=j){
> if (startList.get(i).getLlPoint().getLatitude()==startList.get(j).getLlPoint().getLatitude()&&startList.get(i).getLlPoint().getLongitude()==startList.get(j).getLlPoint().getLongitude()){
> startList.remove(j);
> }
> }
> }
> }
> }
>
> return startList;
> }
>
>
>
> 計算兩點之間的距離
> 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);
> }
算法有帶點粗糙 感興趣的可以使用遞歸算法去算 這個不但可以計算點的距離 還可以計算多路線規劃 設置不同路線顏色等等 看你的需要