Skip to content

Commit ebea93c

Browse files
authored
第15章 第三方开发包
1 parent 6dcbd80 commit ebea93c

38 files changed

+5181
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.baidu.mapapi.overlayutil;
2+
3+
import android.graphics.Color;
4+
import android.util.Log;
5+
6+
import com.baidu.mapapi.map.BaiduMap;
7+
import com.baidu.mapapi.map.BitmapDescriptorFactory;
8+
import com.baidu.mapapi.map.Marker;
9+
import com.baidu.mapapi.map.MarkerOptions;
10+
import com.baidu.mapapi.map.OverlayOptions;
11+
import com.baidu.mapapi.map.Polyline;
12+
import com.baidu.mapapi.map.PolylineOptions;
13+
import com.baidu.mapapi.model.LatLng;
14+
import com.baidu.mapapi.search.busline.BusLineResult;
15+
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
19+
/**
20+
* 用于显示一条公交详情结果的Overlay
21+
*/
22+
public class BusLineOverlay extends OverlayManager {
23+
24+
private BusLineResult mBusLineResult = null;
25+
26+
/**
27+
* 构造函数
28+
*
29+
* @param baiduMap
30+
* 该BusLineOverlay所引用的 BaiduMap 对象
31+
*/
32+
public BusLineOverlay(BaiduMap baiduMap) {
33+
super(baiduMap);
34+
}
35+
36+
/**
37+
* 设置公交线数据
38+
*
39+
* @param result
40+
* 公交线路结果数据
41+
*/
42+
public void setData(BusLineResult result) {
43+
this.mBusLineResult = result;
44+
}
45+
46+
@Override
47+
public final List<OverlayOptions> getOverlayOptions() {
48+
49+
if (mBusLineResult == null || mBusLineResult.getStations() == null) {
50+
return null;
51+
}
52+
List<OverlayOptions> overlayOptionses = new ArrayList<OverlayOptions>();
53+
for (BusLineResult.BusStation station : mBusLineResult.getStations()) {
54+
overlayOptionses.add(new MarkerOptions()
55+
.position(station.getLocation())
56+
.zIndex(10)
57+
.anchor(0.5f, 0.5f)
58+
.icon(BitmapDescriptorFactory
59+
.fromAssetWithDpi("Icon_bus_station.png")));
60+
}
61+
62+
List<LatLng> points = new ArrayList<LatLng>();
63+
for (BusLineResult.BusStep step : mBusLineResult.getSteps()) {
64+
if (step.getWayPoints() != null) {
65+
points.addAll(step.getWayPoints());
66+
}
67+
}
68+
if (points.size() > 0) {
69+
overlayOptionses
70+
.add(new PolylineOptions().width(10)
71+
.color(Color.argb(178, 0, 78, 255)).zIndex(0)
72+
.points(points));
73+
}
74+
return overlayOptionses;
75+
}
76+
77+
/**
78+
* 覆写此方法以改变默认点击行为
79+
*
80+
* @param index
81+
* 被点击的站点在
82+
* {@link com.baidu.mapapi.search.busline.BusLineResult#getStations()}
83+
* 中的索引
84+
* @return 是否处理了该点击事件
85+
*/
86+
public boolean onBusStationClick(int index) {
87+
if (mBusLineResult.getStations() != null
88+
&& mBusLineResult.getStations().get(index) != null) {
89+
Log.i("baidumapsdk", "BusLineOverlay onBusStationClick");
90+
}
91+
return false;
92+
}
93+
94+
public final boolean onMarkerClick(Marker marker) {
95+
if (mOverlayList != null && mOverlayList.contains(marker)) {
96+
return onBusStationClick(mOverlayList.indexOf(marker));
97+
} else {
98+
return false;
99+
}
100+
101+
}
102+
103+
@Override
104+
public boolean onPolylineClick(Polyline polyline) {
105+
// TODO Auto-generated method stub
106+
return false;
107+
}
108+
}

0 commit comments

Comments
 (0)