lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package com.zhaoyang.driver.base.gaode.gpsnav.overlay;
 
import android.content.Context;
 
import com.amap.api.maps.AMap;
import com.amap.api.maps.model.BitmapDescriptor;
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 java.util.ArrayList;
import java.util.List;
 
/**
 * 驾车路线图层类。在高德地图API里,如果要显示驾车路线规划,可以用此类来创建驾车路线图层。如不满足需求,也可以自己创建自定义的驾车路线图层。
 *
 * @since V2.1.0
 */
public class DrivingRouteOverlay extends RouteOverlay {
    private DrivePath drivePath;
    private List<LatLonPoint> throughPointList;
    private List<Marker> throughPointMarkerList = new ArrayList<Marker>();
    private boolean throughPointMarkerVisible = true;
    private Context mContext;
 
    private PolylineOptions mPolylineOptions;
 
 
    /**
     * 通过此构造函数创建驾车路线图层,驾车路线使用一条polyline显示,避免长距离出现卡顿的问题。
     *
     * @param context 当前activity。
     * @param amap    地图对象。
     * @param path    驾车路线规划的一个方案。详见搜索服务模块的路径查询包(com.amap.api.services.route)中的类 <strong><a href="../../../../../../Search/com/amap/api/services/route/DrivePath.html" title="com.amap.api.services.route中的类">DrivePath</a></strong>。
     * @param start   起点。详见搜索服务模块的核心基础包(com.amap.api.services.core)中的类 <strong><a href="../../../../../../Search/com/amap/api/services/core/LatLonPoint.html" title="com.amap.api.services.core中的类">LatLonPoint</a></strong>。
     * @param end     终点。详见搜索服务模块的核心基础包(com.amap.api.services.core)中的类 <strong><a href="../../../../../../Search/com/amap/api/services/core/LatLonPoint.html" title="com.amap.api.services.core中的类">LatLonPoint</a></strong>。
     * @since V2.1.0
     */
    public DrivingRouteOverlay(Context context, AMap amap, DrivePath path,
                               LatLonPoint start, LatLonPoint end) {
        this(context, amap, path, start, end, null);
        mContext = context;
    }
 
    /**
     * 通过此构造函数创建带有途经点的驾车路线图层。
     *
     * @param context          当前activity。
     * @param amap             地图对象。
     * @param path             驾车路线规划的一个方案。详见搜索服务模块的路径查询包(com.amap.api.services.route)中的类 <strong><a href="../../../../../../Search/com/amap/api/services/route/DrivePath.html" title="com.amap.api.services.route中的类">DrivePath</a></strong>。
     * @param start            起点。详见搜索服务模块的核心基础包(com.amap.api.services.core)中的类 <strong><a href="../../../../../../Search/com/amap/api/services/core/LatLonPoint.html" title="com.amap.api.services.core中的类">LatLonPoint</a></strong>。
     * @param end              终点。详见搜索服务模块的核心基础包(com.amap.api.services.core)中的类<strong><a href="../../../../../../Search/com/amap/api/services/core/LatLonPoint.html" title="com.amap.api.services.core中的类">LatLonPoint</a></strong>。
     * @param throughPointList 途经点列表,详见搜索服务模块的核心基础包(com.amap.api.services.core)中的类<strong><a href="../../../../../../Search/com/amap/api/services/core/LatLonPoint.html" title="com.amap.api.services.core中的类">LatLonPoint</a></strong>。
     * @since V2.3.1
     */
    public DrivingRouteOverlay(Context context, AMap amap, DrivePath path,
                               LatLonPoint start, LatLonPoint end,
                               List<LatLonPoint> throughPointList) {
        super(context);
        this.mAMap = amap;
        this.drivePath = path;
        startPoint = AMapServicesUtil.convertToLatLng(start);
        endPoint = AMapServicesUtil.convertToLatLng(end);
        this.throughPointList = throughPointList;
        mContext = context;
    }
 
    /**
     * 添加驾车路线到地图中。
     *
     * @since V2.1.0
     */
    public void addToMap(int color) {
 
        initPolylineOptions(color);
        try {
            List<DriveStep> drivePaths = drivePath.getSteps();
            for (int i = 0; i < drivePaths.size(); i++) {
                DriveStep driveStep = drivePaths.get(i);
                LatLng latLng = AMapServicesUtil.convertToLatLng(getFirstDrivePoint(driveStep));
                if (i < drivePaths.size() - 1) {
                    // 连接起点
                    if (i == 0) {
                        addDrivingPolyLine(startPoint, latLng);
                    }
//                    checkDistanceToNextStep(driveStep, drivePaths.get(i + 1));
                }
                 // 中间路段
                addDrivingStationMarkers(driveStep, latLng);
                addDrivingPolyLine(driveStep);
 
                // 连接终点,放在最后
                if(i == drivePaths.size() - 1) {
                    addDrivingPolyLine(getLastDrivePoint(driveStep), endPoint);
                }
            }
            addStartAndEndMarker();
//            addThroughPointMarker();
 
            showPolyline();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 初始化线段属性
     */
    private void initPolylineOptions(int color) {
 
        mPolylineOptions = null;
 
        mPolylineOptions = new PolylineOptions();
        mPolylineOptions.color(color).width(getRouteWidth());
    }
 
    private void showPolyline() {
        addPolyLine(mPolylineOptions);
    }
 
    /**
     * 检查这一步的最后一点和下一步的起始点之间是否存在空隙
     *
     * @param curStep
     * @param nextStep
     */
    private void checkDistanceToNextStep(DriveStep curStep,
                                         DriveStep nextStep) {
        LatLonPoint lastPoint = getLastDrivePoint(curStep);
        LatLonPoint nextFirstPoint = getFirstDrivePoint(nextStep);
        if (!(lastPoint.equals(nextFirstPoint))) {
            addDrivingPolyLine(lastPoint, nextFirstPoint);
        }
    }
 
    /**
     * @param driveStep
     * @return
     */
    private LatLonPoint getFirstDrivePoint(DriveStep driveStep) {
        return driveStep
                .getPolyline().get(0);
    }
 
    /**
     * @param driveStep
     * @return
     */
    private LatLonPoint getLastDrivePoint(DriveStep driveStep) {
        return driveStep
                .getPolyline().get(
                        driveStep.getPolyline().size() - 1);
    }
 
 
    private void addDrivingPolyLine(LatLonPoint pointFrom, LatLonPoint pointTo) {
        addDrivingPolyLine(AMapServicesUtil.convertToLatLng(pointFrom), AMapServicesUtil.convertToLatLng(pointTo));
    }
 
    private void addDrivingPolyLine(LatLng latLngFrom, LatLonPoint pointTo) {
        addDrivingPolyLine(latLngFrom, AMapServicesUtil.convertToLatLng(pointTo));
    }
 
    private void addDrivingPolyLine(LatLonPoint pointFrom, LatLng latLngTo) {
        addDrivingPolyLine(AMapServicesUtil.convertToLatLng(pointFrom), latLngTo);
    }
 
 
    private void addDrivingPolyLine(LatLng latLngFrom, LatLng latLngTo) {
        mPolylineOptions.add(latLngFrom, latLngTo);
//        addPolyLine(new PolylineOptions()
//                .add(latLngFrom, latLngTo)
//                .color(getDriveColor()).width(getRouteWidth()));
    }
 
    /**
     * @param driveStep
     */
    private void addDrivingPolyLine(DriveStep driveStep) {
        mPolylineOptions.addAll(AMapServicesUtil.convertArrList(driveStep.getPolyline()));
//        addPolyLine(new PolylineOptions()
//                .addAll(AMapServicesUtil.convertArrList(driveStep.getPolyline()))
//                .color(getDriveColor()).width(getRouteWidth()));
    }
 
    /**
     * @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()));
    }
 
 
    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")));
                }
            }
        }
    }
 
    @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 BitmapDescriptor getThroughPointBitDes() {
        return null;
       
    }
 
    /**
     * 去掉DriveLineOverlay上所有的Marker。
     *
     * @since V2.1.0
     */
    @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();
        }
    }
}