Commit 250fa551ed18b79535e99328d811ef1e023affca
1 parent
f33024a4
封装地图路线规划
Showing
9 changed files
with
59 additions
and
51 deletions
lib/android/build.gradle
... | ... | @@ -6,7 +6,7 @@ def getExt(prop, fallback) { |
6 | 6 | } |
7 | 7 | |
8 | 8 | buildscript { |
9 | - ext.kotlin_version = '1.5.31' | |
9 | + ext.kotlin_version = '1.7.20' | |
10 | 10 | |
11 | 11 | repositories { |
12 | 12 | mavenCentral() |
... | ... | @@ -39,6 +39,6 @@ android { |
39 | 39 | dependencies { |
40 | 40 | api 'com.facebook.react:react-native:+' |
41 | 41 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" |
42 | - implementation 'com.amap.api:3dmap:9.4.0' | |
43 | - implementation 'com.amap.api:search:9.4.0' | |
42 | + implementation 'com.amap.api:3dmap:9.5.0' | |
43 | + implementation 'com.amap.api:search:9.4.5' | |
44 | 44 | } | ... | ... |
lib/android/src/main/java/cn/feewee/amap3d/map_view/route/DrivingRoute.kt
... | ... | @@ -4,15 +4,17 @@ import android.content.Context |
4 | 4 | import android.graphics.Color |
5 | 5 | import android.widget.Toast |
6 | 6 | import cn.feewee.amap3d.* |
7 | -import cn.feewee.amap3d.R | |
8 | 7 | import com.amap.api.maps.AMap |
9 | -import com.amap.api.maps.model.* | |
8 | +import com.amap.api.maps.model.LatLng | |
9 | +import com.amap.api.maps.model.Marker | |
10 | +import com.amap.api.maps.model.MarkerOptions | |
11 | +import com.amap.api.maps.model.PolylineOptions | |
10 | 12 | import com.amap.api.services.core.AMapException |
11 | 13 | import com.amap.api.services.route.* |
12 | 14 | import com.facebook.react.bridge.ReadableArray |
13 | 15 | |
14 | -class DrivingRoute(context: Context) : RouteOverlay(context) { | |
15 | - private var drivePath: DrivePathV2? = null | |
16 | +class DrivingRoute(context: Context) : RouteOverlay(context), RouteSearch.OnRouteSearchListener { | |
17 | + private var drivePath: DrivePath? = null | |
16 | 18 | private var throughPointList: List<LatLng> = emptyList() |
17 | 19 | private var isColorfulline = true |
18 | 20 | private var throughPointMarkerList: ArrayList<Marker> = ArrayList() |
... | ... | @@ -22,7 +24,8 @@ class DrivingRoute(context: Context) : RouteOverlay(context) { |
22 | 24 | |
23 | 25 | init { |
24 | 26 | try { |
25 | - mRouteSearch = RouteSearchV2(context) | |
27 | + mRouteSearch = RouteSearch(context) | |
28 | + mRouteSearch!!.setRouteSearchListener(this) | |
26 | 29 | } catch (e: AMapException) { |
27 | 30 | e.printStackTrace() |
28 | 31 | } |
... | ... | @@ -162,7 +165,7 @@ class DrivingRoute(context: Context) : RouteOverlay(context) { |
162 | 165 | * @param driveStep |
163 | 166 | * @param latLng |
164 | 167 | */ |
165 | - private fun addDrivingStationMarkers(driveStep: DriveStepV2, latLng: LatLng) { | |
168 | + private fun addDrivingStationMarkers(driveStep: DriveStep, latLng: LatLng) { | |
166 | 169 | addStationMarker( |
167 | 170 | MarkerOptions() |
168 | 171 | .position(latLng) |
... | ... | @@ -199,16 +202,15 @@ class DrivingRoute(context: Context) : RouteOverlay(context) { |
199 | 202 | val fromAndTo = createFromAndTo() ?: return |
200 | 203 | try { |
201 | 204 | showProgressDialog() |
202 | - val driveRouteResult = mRouteSearch!!.calculateDriveRoute( | |
203 | - RouteSearchV2.DriveRouteQuery( | |
205 | + mRouteSearch!!.calculateDriveRouteAsyn( | |
206 | + RouteSearch.DriveRouteQuery( | |
204 | 207 | fromAndTo, |
205 | - RouteSearchV2.DrivingStrategy.DEFAULT, | |
208 | + RouteSearch.DRIVEING_PLAN_FASTEST_SHORTEST, | |
206 | 209 | throughPointList.map { r -> r.convertToLatLonPoint() }, |
207 | 210 | null, |
208 | 211 | null |
209 | 212 | ) |
210 | 213 | ) |
211 | - onDriveRouteSearched(driveRouteResult) | |
212 | 214 | } catch (e: AMapException) { |
213 | 215 | dissmissProgressDialog() |
214 | 216 | showerror(context, e.errorCode) |
... | ... | @@ -216,10 +218,9 @@ class DrivingRoute(context: Context) : RouteOverlay(context) { |
216 | 218 | |
217 | 219 | } |
218 | 220 | |
219 | - private fun onDriveRouteSearched(result: DriveRouteResultV2) { | |
220 | - dissmissProgressDialog() | |
221 | + private fun onDriveRouteSearched(result: DriveRouteResult?) { | |
221 | 222 | map.clear() |
222 | - if (result.paths != null) { | |
223 | + if (result?.paths != null) { | |
223 | 224 | if (result.paths.size > 0) { |
224 | 225 | drivePath = result.paths[0] ?: return |
225 | 226 | remove() |
... | ... | @@ -255,4 +256,21 @@ class DrivingRoute(context: Context) : RouteOverlay(context) { |
255 | 256 | searchRouteResult() |
256 | 257 | } |
257 | 258 | |
259 | + override fun onDriveRouteSearched(result: DriveRouteResult?, errorCode: Int) { | |
260 | + dissmissProgressDialog() | |
261 | + if (errorCode == AMapException.CODE_AMAP_SUCCESS) { | |
262 | + onDriveRouteSearched(result) | |
263 | + } else { | |
264 | + showerror(context, errorCode) | |
265 | + } | |
266 | + } | |
267 | + | |
268 | + override fun onBusRouteSearched(result: BusRouteResult?, errorCode: Int) { | |
269 | + } | |
270 | + | |
271 | + override fun onWalkRouteSearched(result: WalkRouteResult?, errorCode: Int) { | |
272 | + } | |
273 | + | |
274 | + override fun onRideRouteSearched(result: RideRouteResult?, errorCode: Int) { | |
275 | + } | |
258 | 276 | } |
259 | 277 | \ No newline at end of file | ... | ... |
lib/android/src/main/java/cn/feewee/amap3d/map_view/route/DrivingRouteManager.kt
... | ... | @@ -85,7 +85,7 @@ class DrivingRouteManager : SimpleViewManager<DrivingRoute>() { |
85 | 85 | |
86 | 86 | @ReactProp(name = "throughPointIcon") |
87 | 87 | fun setThroughPointIcon(route: DrivingRoute, throughPoint: ReadableMap?) { |
88 | - throughPoint?.let { route.setThroughPointBitDes(it) } | |
88 | + throughPoint?.let { route.setThroughBit(it) } | |
89 | 89 | } |
90 | 90 | |
91 | 91 | @ReactProp(name = "roadLine") | ... | ... |
lib/android/src/main/java/cn/feewee/amap3d/map_view/route/RouteOverlay.kt
... | ... | @@ -28,7 +28,7 @@ abstract class RouteOverlay(context: Context) : ReactViewGroup(context), Overlay |
28 | 28 | private var roadColor: Int = Color.parseColor("#537edc") |
29 | 29 | private var progDialog: AlertDialog? = null |
30 | 30 | |
31 | - protected var mRouteSearch: RouteSearchV2? = null | |
31 | + protected var mRouteSearch: RouteSearch? = null | |
32 | 32 | |
33 | 33 | abstract fun searchRouteResult() |
34 | 34 | |
... | ... | @@ -263,10 +263,10 @@ abstract class RouteOverlay(context: Context) : ReactViewGroup(context), Overlay |
263 | 263 | if (startPoint != null) { |
264 | 264 | try { |
265 | 265 | val bounds = getLatLngBounds() |
266 | - map.animateCamera( | |
267 | - CameraUpdateFactory | |
268 | - .newLatLngBounds(bounds, 100) | |
269 | - ) | |
266 | + val newLatLngBounds = CameraUpdateFactory | |
267 | + .newLatLngBounds(bounds, 100) | |
268 | + | |
269 | + map.animateCamera(newLatLngBounds) | |
270 | 270 | } catch (e: Throwable) { |
271 | 271 | e.printStackTrace() |
272 | 272 | } |
... | ... | @@ -304,7 +304,7 @@ abstract class RouteOverlay(context: Context) : ReactViewGroup(context), Overlay |
304 | 304 | /** |
305 | 305 | * 开始搜索路径规划方案 |
306 | 306 | */ |
307 | - protected fun createFromAndTo(): RouteSearchV2.FromAndTo? { | |
307 | + protected fun createFromAndTo(): RouteSearch.FromAndTo? { | |
308 | 308 | if (startPoint == null) { |
309 | 309 | show(context, "起点未设置") |
310 | 310 | return null |
... | ... | @@ -313,7 +313,7 @@ abstract class RouteOverlay(context: Context) : ReactViewGroup(context), Overlay |
313 | 313 | show(context, "终点未设置") |
314 | 314 | return null |
315 | 315 | } |
316 | - return RouteSearchV2.FromAndTo( | |
316 | + return RouteSearch.FromAndTo( | |
317 | 317 | startPoint!!.convertToLatLonPoint(), |
318 | 318 | endPoint!!.convertToLatLonPoint() |
319 | 319 | ) |
... | ... | @@ -331,7 +331,7 @@ abstract class RouteOverlay(context: Context) : ReactViewGroup(context), Overlay |
331 | 331 | */ |
332 | 332 | protected fun showProgressDialog() { |
333 | 333 | if (progDialog == null) { |
334 | - progressBarCircleDialog() | |
334 | + progDialog = progressBarCircleDialog() | |
335 | 335 | } |
336 | 336 | progDialog?.setCanceledOnTouchOutside(false); |
337 | 337 | progDialog?.setCancelable(false); |
... | ... | @@ -339,17 +339,17 @@ abstract class RouteOverlay(context: Context) : ReactViewGroup(context), Overlay |
339 | 339 | } |
340 | 340 | |
341 | 341 | |
342 | - private fun progressBarCircleDialog() { | |
342 | + private fun progressBarCircleDialog(): AlertDialog { | |
343 | 343 | val builder = AlertDialog.Builder( |
344 | 344 | context, |
345 | 345 | AlertDialog.THEME_HOLO_LIGHT |
346 | 346 | ) |
347 | - val inflater = LayoutInflater.from(context); | |
347 | + val inflater = LayoutInflater.from(context.applicationContext); | |
348 | 348 | @SuppressLint("InflateParams") |
349 | 349 | val view: View = inflater.inflate(R.layout.dialog_progressbar_circle, null); |
350 | 350 | val tv: TextView = view.findViewById(R.id.tv); |
351 | 351 | tv.text = "正在搜索..."; |
352 | 352 | builder.setView(view); |
353 | - progDialog = builder.create(); | |
353 | + return builder.create(); | |
354 | 354 | } |
355 | 355 | } |
356 | 356 | \ No newline at end of file | ... | ... |
lib/android/src/main/res/layout/dialog_progressbar_circle.xml
... | ... | @@ -15,14 +15,13 @@ |
15 | 15 | android:layout_margin="5dp" |
16 | 16 | android:indeterminateDuration="300" /> |
17 | 17 | |
18 | - <!--android:indeterminateDrawable="@drawable/progress_bar"--> | |
19 | - | |
20 | 18 | <TextView |
21 | 19 | android:id="@+id/tv" |
22 | 20 | android:layout_width="0dp" |
23 | 21 | android:layout_height="wrap_content" |
24 | 22 | android:layout_weight="1" |
23 | + android:paddingVertical="5dp" | |
25 | 24 | android:gravity="left|center" |
26 | - android:textSize="22sp" | |
25 | + android:textSize="16sp" | |
27 | 26 | tools:ignore="RtlHardcoded" /> |
28 | 27 | </LinearLayout> | ... | ... |
lib/src/marker.tsx
1 | 1 | import * as React from "react"; |
2 | -import { | |
3 | - ImageSourcePropType, | |
4 | - NativeSyntheticEvent, | |
5 | - requireNativeComponent, | |
6 | - View, | |
7 | - ViewStyle, | |
8 | -} from "react-native"; | |
2 | +import { ImageSourcePropType, NativeSyntheticEvent, requireNativeComponent, View, ViewStyle } from "react-native"; | |
9 | 3 | // @ts-ignore |
10 | 4 | import resolveAssetSource from "react-native/Libraries/Image/resolveAssetSource"; |
11 | 5 | import Component from "./component"; |
... | ... | @@ -14,8 +8,8 @@ import { LatLng, Point } from "./types"; |
14 | 8 | export interface MarkerProps { |
15 | 9 | /** |
16 | 10 | * 坐标 |
17 | - */ position: LatLng; | |
18 | - | |
11 | + */ | |
12 | + position: LatLng; | |
19 | 13 | /** |
20 | 14 | * 图标 |
21 | 15 | */ |
... | ... | @@ -110,7 +104,7 @@ export default class extends Component<MarkerProps> { |
110 | 104 | render() { |
111 | 105 | const props = { ...this.props }; |
112 | 106 | // @ts-ignore android 不能用 position 作为属性,会发生冲突 |
113 | - props.latLng = props.position | |
107 | + props.latLng = props.position; | |
114 | 108 | // @ts-ignore |
115 | 109 | delete props.position; |
116 | 110 | if (props.children) { | ... | ... |
lib/src/polyline.tsx
... | ... | @@ -54,7 +54,7 @@ export interface PolylineProps { |
54 | 54 | } |
55 | 55 | |
56 | 56 | export default class extends React.PureComponent<PolylineProps> { |
57 | - static defaultProps = { colors: [], with: 14 }; | |
57 | + static defaultProps = { colors: [], with: 16 }; | |
58 | 58 | |
59 | 59 | render() { |
60 | 60 | const props = { | ... | ... |
lib/src/route/driving-route.tsx
... | ... | @@ -35,10 +35,6 @@ export interface DrivingRouteProps { |
35 | 35 | */ |
36 | 36 | roadLine?: ImageSourcePropType; |
37 | 37 | /** |
38 | - * 驾车图标 | |
39 | - */ | |
40 | - driveIcon?: ImageSourcePropType; | |
41 | - /** | |
42 | 38 | * 起点图标 |
43 | 39 | */ |
44 | 40 | startIcon?: ImageSourcePropType; |
... | ... | @@ -57,12 +53,13 @@ export interface DrivingRouteProps { |
57 | 53 | } |
58 | 54 | |
59 | 55 | export default class extends Component<DrivingRouteProps> { |
60 | - static defaultProps = { with: 14, throughPointVisible: true }; | |
56 | + name = name; | |
57 | + static defaultProps = { with: 16, throughPointVisible: true }; | |
61 | 58 | |
62 | 59 | /** |
63 | 60 | * 路线规划 |
64 | 61 | */ |
65 | - routePlan(start: LatLng, end: LatLng, throughPointList: LatLng[] = []) { | |
62 | + public routePlan(start: LatLng, end: LatLng, throughPointList: LatLng[] = []) { | |
66 | 63 | this.invoke("searchRoute", [start, end, throughPointList]); |
67 | 64 | } |
68 | 65 | |
... | ... | @@ -73,7 +70,6 @@ export default class extends Component<DrivingRouteProps> { |
73 | 70 | return ( |
74 | 71 | <NativeDrivingRoute |
75 | 72 | {...props} |
76 | - driveIcon={resolveAssetSource(props.driveIcon)} | |
77 | 73 | startIcon={resolveAssetSource(props.startIcon)} |
78 | 74 | endIcon={resolveAssetSource(props.endIcon)} |
79 | 75 | throughPointIcon={resolveAssetSource(props.throughPointIcon)} |
... | ... | @@ -83,4 +79,5 @@ export default class extends Component<DrivingRouteProps> { |
83 | 79 | } |
84 | 80 | } |
85 | 81 | |
86 | -const NativeDrivingRoute = requireNativeComponent<DrivingRouteProps>("DrivingRoute"); | |
82 | +const name = "DrivingRoute"; | |
83 | +const NativeDrivingRoute = requireNativeComponent<DrivingRouteProps>(name); | ... | ... |