lmw
2023-03-11 4df5bb59e5fe9f9d140e5610f7772dd8a05a28d4
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
package com.fuban.driver.base.gaode.gpsnav.overlay;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.widget.TextView;
 
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
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.Polyline;
import com.amap.api.maps.model.PolylineOptions;
 
import java.util.ArrayList;
import java.util.List;
 
class RouteOverlay {
    protected List<Marker> stationMarkers = new ArrayList<Marker>();
    protected List<Polyline> allPolyLines = new ArrayList<Polyline>();
//    protected Marker startMarker;
    protected Marker endMarker;
    protected LatLng startPoint;
    protected LatLng endPoint;
    protected AMap mAMap;
    private Context mContext;
    private Bitmap startBit, endBit, busBit, walkBit, driveBit;
    protected boolean nodeIconVisible = true;
    protected TextView tv_content;
 
    public RouteOverlay(Context context) {
        mContext = context;
    }
 
    /**
     * 去掉BusRouteOverlay上所有的Marker。
     * @since V2.1.0
     */
    public void removeFromMap() {
//        if (startMarker != null) {
//            startMarker.remove();
//
//        }
        if (endMarker != null) {
            endMarker.remove();
        }
        for (Marker marker : stationMarkers) {
            marker.remove();
        }
        for (Polyline line : allPolyLines) {
            line.remove();
        }
        destroyBit();
    }
 
    private void destroyBit() {
        if (startBit != null) {
            startBit.recycle();
            startBit = null;
        }
        if (endBit != null) {
            endBit.recycle();
            endBit = null;
        }
        if (busBit != null) {
            busBit.recycle();
            busBit = null;
        }
        if (walkBit != null) {
            walkBit.recycle();
            walkBit = null;
        }
        if (driveBit != null) {
            driveBit.recycle();
            driveBit = null;
        }
    }
    /**
     * 给起点Marker设置图标,并返回更换图标的图片。如不用默认图片,需要重写此方法。
     * @return 更换的Marker图片。
     * @since V2.1.0
     */
    protected BitmapDescriptor getStartBitmapDescriptor() {
//        View view = View.inflate(mContext, R.layout.view_location_layout, null);
//        tv_content = (TextView) view.findViewById(R.id.tv_content);
        return null;
    }
    /**
     * 给终点Marker设置图标,并返回更换图标的图片。如不用默认图片,需要重写此方法。
     * @return 更换的Marker图片。
     * @since V2.1.0
     */
    protected BitmapDescriptor getEndBitmapDescriptor() {
//        return BitmapDescriptorFactory.fromResource(R.mipmap.end_loc_bg);
        return null;
    }
    /**
     * 给公交Marker设置图标,并返回更换图标的图片。如不用默认图片,需要重写此方法。
     * @return 更换的Marker图片。
     * @since V2.1.0
     */
    protected BitmapDescriptor getBusBitmapDescriptor() {
        return null;
    }
    /**
     * 给步行Marker设置图标,并返回更换图标的图片。如不用默认图片,需要重写此方法。
     * @return 更换的Marker图片。
     * @since V2.1.0
     */
    protected BitmapDescriptor getWalkBitmapDescriptor() {
        return null;
    }
 
    protected BitmapDescriptor getDriveBitmapDescriptor() {
        return null;
    }
 
    protected void addStartAndEndMarker() {
//        startMarker = mAMap.addMarker((new MarkerOptions())
//                .position(startPoint).icon(getStartBitmapDescriptor())
//                .title("\u8D77\u70B9"));
        // startMarker.showInfoWindow();
 
//        endMarker = mAMap.addMarker((new MarkerOptions()).position(endPoint)
//                .icon(getEndBitmapDescriptor()).title("\u7EC8\u70B9"));
        // mAMap.moveCamera(CameraUpdateFactory.newLatLngZoom(startPoint,
        // getShowRouteZoom()));
    }
    /**
     * 移动镜头到当前的视角。
     * @since V2.1.0
     */
    public void zoomToSpan(int left,int top,int right,int bottom) {
        if (startPoint != null) {
            if (mAMap == null)
                return;
            try {
                LatLngBounds bounds = getLatLngBounds();
                mAMap.animateCamera(CameraUpdateFactory
                        .newLatLngBoundsRect(bounds, left,right,top,bottom));
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
 
    protected LatLngBounds getLatLngBounds() {
        LatLngBounds.Builder b = LatLngBounds.builder();
        b.include(new LatLng(startPoint.latitude, startPoint.longitude));
        b.include(new LatLng(endPoint.latitude, endPoint.longitude));
        return b.build();
    }
    /**
     * 路段节点图标控制显示接口。
     * @param visible true为显示节点图标,false为不显示。
     * @since V2.3.1
     */
    public void setNodeIconVisibility(boolean visible) {
        try {
            nodeIconVisible = visible;
            if (this.stationMarkers != null && this.stationMarkers.size() > 0) {
                for (int i = 0; i < this.stationMarkers.size(); i++) {
                    this.stationMarkers.get(i).setVisible(visible);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    
    protected void addStationMarker(MarkerOptions options) {
        if(options == null) {
            return;
        }
        Marker marker = mAMap.addMarker(options);
        if(marker != null) {
            stationMarkers.add(marker);
        }
        
    }
 
    protected void addPolyLine(PolylineOptions options) {
        if(options == null) {
            return;
        }
        Polyline polyline = mAMap.addPolyline(options);
        allPolyLines.add(polyline);
    }
    
    protected float getRouteWidth() {
        return 18f;
    }
 
    protected int getWalkColor() {
        return Color.parseColor("#6db74d");
    }
 
    /**
     * 自定义路线颜色。
     * return 自定义路线颜色。
     * @since V2.2.1
     */
    protected int getBusColor() {
        return Color.parseColor("#537edc");
    }
 
    protected int getDriveColor() {
        return Color.parseColor("#537edc");
    }
 
    // protected int getShowRouteZoom() {
    // return 15;
    // }
}