1
+ /*
2
+ * Copyright (C) 2016 Baidu, Inc. All Rights Reserved.
3
+ */
4
+ package com .baidu .mapapi .overlayutil ;
5
+
6
+ import android .graphics .Color ;
7
+ import android .os .Bundle ;
8
+ import android .util .Log ;
9
+
10
+ import com .baidu .mapapi .map .BaiduMap ;
11
+ import com .baidu .mapapi .map .BitmapDescriptor ;
12
+ import com .baidu .mapapi .map .BitmapDescriptorFactory ;
13
+ import com .baidu .mapapi .map .Marker ;
14
+ import com .baidu .mapapi .map .MarkerOptions ;
15
+ import com .baidu .mapapi .map .Overlay ;
16
+ import com .baidu .mapapi .map .OverlayOptions ;
17
+ import com .baidu .mapapi .map .Polyline ;
18
+ import com .baidu .mapapi .map .PolylineOptions ;
19
+ import com .baidu .mapapi .model .LatLng ;
20
+ import com .baidu .mapapi .search .route .BikingRouteLine ;
21
+
22
+ import java .util .ArrayList ;
23
+ import java .util .List ;
24
+
25
+ /**
26
+ * 用于显示骑行路线的Overlay
27
+ */
28
+ public class BikingRouteOverlay extends OverlayManager {
29
+
30
+ private BikingRouteLine mRouteLine = null ;
31
+
32
+ public BikingRouteOverlay (BaiduMap baiduMap ) {
33
+ super (baiduMap );
34
+ }
35
+
36
+ /**
37
+ * 设置路线数据。
38
+ *
39
+ * @param line
40
+ * 路线数据
41
+ */
42
+ public void setData (BikingRouteLine line ) {
43
+ mRouteLine = line ;
44
+ }
45
+
46
+ @ Override
47
+ public final List <OverlayOptions > getOverlayOptions () {
48
+ if (mRouteLine == null ) {
49
+ return null ;
50
+ }
51
+
52
+ List <OverlayOptions > overlayList = new ArrayList <OverlayOptions >();
53
+ if (mRouteLine .getAllStep () != null
54
+ && mRouteLine .getAllStep ().size () > 0 ) {
55
+ for (BikingRouteLine .BikingStep step : mRouteLine .getAllStep ()) {
56
+ Bundle b = new Bundle ();
57
+ b .putInt ("index" , mRouteLine .getAllStep ().indexOf (step ));
58
+ if (step .getEntrance () != null ) {
59
+ overlayList .add ((new MarkerOptions ())
60
+ .position (step .getEntrance ().getLocation ())
61
+ .rotate ((360 - step .getDirection ()))
62
+ .zIndex (10 )
63
+ .anchor (0.5f , 0.5f )
64
+ .extraInfo (b )
65
+ .icon (BitmapDescriptorFactory
66
+ .fromAssetWithDpi ("Icon_line_node.png" )));
67
+ }
68
+
69
+ // 最后路段绘制出口点
70
+ if (mRouteLine .getAllStep ().indexOf (step ) == (mRouteLine
71
+ .getAllStep ().size () - 1 ) && step .getExit () != null ) {
72
+ overlayList .add ((new MarkerOptions ())
73
+ .position (step .getExit ().getLocation ())
74
+ .anchor (0.5f , 0.5f )
75
+ .zIndex (10 )
76
+ .icon (BitmapDescriptorFactory
77
+ .fromAssetWithDpi ("Icon_line_node.png" )));
78
+
79
+ }
80
+ }
81
+ }
82
+ // starting
83
+ if (mRouteLine .getStarting () != null ) {
84
+ overlayList .add ((new MarkerOptions ())
85
+ .position (mRouteLine .getStarting ().getLocation ())
86
+ .icon (getStartMarker () != null ? getStartMarker () :
87
+ BitmapDescriptorFactory
88
+ .fromAssetWithDpi ("Icon_start.png" )).zIndex (10 ));
89
+ }
90
+ // terminal
91
+ if (mRouteLine .getTerminal () != null ) {
92
+ overlayList
93
+ .add ((new MarkerOptions ())
94
+ .position (mRouteLine .getTerminal ().getLocation ())
95
+ .icon (getTerminalMarker () != null ? getTerminalMarker () :
96
+ BitmapDescriptorFactory
97
+ .fromAssetWithDpi ("Icon_end.png" ))
98
+ .zIndex (10 ));
99
+ }
100
+
101
+ // poly line list
102
+ if (mRouteLine .getAllStep () != null
103
+ && mRouteLine .getAllStep ().size () > 0 ) {
104
+ LatLng lastStepLastPoint = null ;
105
+ for (BikingRouteLine .BikingStep step : mRouteLine .getAllStep ()) {
106
+ List <LatLng > watPoints = step .getWayPoints ();
107
+ if (watPoints != null ) {
108
+ List <LatLng > points = new ArrayList <LatLng >();
109
+ if (lastStepLastPoint != null ) {
110
+ points .add (lastStepLastPoint );
111
+ }
112
+ points .addAll (watPoints );
113
+ overlayList .add (new PolylineOptions ().points (points ).width (10 )
114
+ .color (getLineColor () != 0 ? getLineColor () : Color .argb (178 , 0 , 78 , 255 )).zIndex (0 ));
115
+ lastStepLastPoint = watPoints .get (watPoints .size () - 1 );
116
+ }
117
+ }
118
+
119
+ }
120
+
121
+ return overlayList ;
122
+ }
123
+
124
+ /**
125
+ * 覆写此方法以改变默认起点图标
126
+ *
127
+ * @return 起点图标
128
+ */
129
+ public BitmapDescriptor getStartMarker () {
130
+ return null ;
131
+ }
132
+ public int getLineColor () {
133
+ return 0 ;
134
+ }
135
+ /**
136
+ * 覆写此方法以改变默认终点图标
137
+ *
138
+ * @return 终点图标
139
+ */
140
+ public BitmapDescriptor getTerminalMarker () {
141
+ return null ;
142
+ }
143
+
144
+ /**
145
+ * 处理点击事件
146
+ *
147
+ * @param i
148
+ * 被点击的step在
149
+ * {@link com.baidu.mapapi.search.route.BikingRouteLine#getAllStep()}
150
+ * 中的索引
151
+ * @return 是否处理了该点击事件
152
+ */
153
+ public boolean onRouteNodeClick (int i ) {
154
+ if (mRouteLine .getAllStep () != null
155
+ && mRouteLine .getAllStep ().get (i ) != null ) {
156
+ Log .i ("baidumapsdk" , "BikingRouteOverlay onRouteNodeClick" );
157
+ }
158
+ return false ;
159
+ }
160
+
161
+ @ Override
162
+ public final boolean onMarkerClick (Marker marker ) {
163
+ for (Overlay mMarker : mOverlayList ) {
164
+ if (mMarker instanceof Marker && mMarker .equals (marker )) {
165
+ if (marker .getExtraInfo () != null ) {
166
+ onRouteNodeClick (marker .getExtraInfo ().getInt ("index" ));
167
+ }
168
+ }
169
+ }
170
+ return true ;
171
+ }
172
+
173
+ @ Override
174
+ public boolean onPolylineClick (Polyline polyline ) {
175
+ // TODO Auto-generated method stub
176
+ return false ;
177
+ }
178
+ }
0 commit comments