From 250fa551ed18b79535e99328d811ef1e023affca Mon Sep 17 00:00:00 2001 From: Kurisu Date: Tue, 25 Oct 2022 16:14:20 +0800 Subject: [PATCH] 封装地图路线规划 --- lib/android/build.gradle | 6 +++--- lib/android/src/main/java/cn/feewee/amap3d/map_view/route/DrivingRoute.kt | 44 +++++++++++++++++++++++++++++++------------- lib/android/src/main/java/cn/feewee/amap3d/map_view/route/DrivingRouteManager.kt | 2 +- lib/android/src/main/java/cn/feewee/amap3d/map_view/route/RouteOverlay.kt | 22 +++++++++++----------- lib/android/src/main/res/layout/dialog_progressbar_circle.xml | 5 ++--- lib/src/marker.tsx | 14 ++++---------- lib/src/polyline.tsx | 2 +- lib/src/route/driving-route.tsx | 13 +++++-------- package.json | 2 +- 9 files changed, 59 insertions(+), 51 deletions(-) diff --git a/lib/android/build.gradle b/lib/android/build.gradle index 7d7c78f..b36af04 100644 --- a/lib/android/build.gradle +++ b/lib/android/build.gradle @@ -6,7 +6,7 @@ def getExt(prop, fallback) { } buildscript { - ext.kotlin_version = '1.5.31' + ext.kotlin_version = '1.7.20' repositories { mavenCentral() @@ -39,6 +39,6 @@ android { dependencies { api 'com.facebook.react:react-native:+' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation 'com.amap.api:3dmap:9.4.0' - implementation 'com.amap.api:search:9.4.0' + implementation 'com.amap.api:3dmap:9.5.0' + implementation 'com.amap.api:search:9.4.5' } diff --git a/lib/android/src/main/java/cn/feewee/amap3d/map_view/route/DrivingRoute.kt b/lib/android/src/main/java/cn/feewee/amap3d/map_view/route/DrivingRoute.kt index e58a222..3c53cdc 100644 --- a/lib/android/src/main/java/cn/feewee/amap3d/map_view/route/DrivingRoute.kt +++ b/lib/android/src/main/java/cn/feewee/amap3d/map_view/route/DrivingRoute.kt @@ -4,15 +4,17 @@ import android.content.Context import android.graphics.Color import android.widget.Toast import cn.feewee.amap3d.* -import cn.feewee.amap3d.R import com.amap.api.maps.AMap -import com.amap.api.maps.model.* +import com.amap.api.maps.model.LatLng +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.AMapException import com.amap.api.services.route.* import com.facebook.react.bridge.ReadableArray -class DrivingRoute(context: Context) : RouteOverlay(context) { - private var drivePath: DrivePathV2? = null +class DrivingRoute(context: Context) : RouteOverlay(context), RouteSearch.OnRouteSearchListener { + private var drivePath: DrivePath? = null private var throughPointList: List = emptyList() private var isColorfulline = true private var throughPointMarkerList: ArrayList = ArrayList() @@ -22,7 +24,8 @@ class DrivingRoute(context: Context) : RouteOverlay(context) { init { try { - mRouteSearch = RouteSearchV2(context) + mRouteSearch = RouteSearch(context) + mRouteSearch!!.setRouteSearchListener(this) } catch (e: AMapException) { e.printStackTrace() } @@ -162,7 +165,7 @@ class DrivingRoute(context: Context) : RouteOverlay(context) { * @param driveStep * @param latLng */ - private fun addDrivingStationMarkers(driveStep: DriveStepV2, latLng: LatLng) { + private fun addDrivingStationMarkers(driveStep: DriveStep, latLng: LatLng) { addStationMarker( MarkerOptions() .position(latLng) @@ -199,16 +202,15 @@ class DrivingRoute(context: Context) : RouteOverlay(context) { val fromAndTo = createFromAndTo() ?: return try { showProgressDialog() - val driveRouteResult = mRouteSearch!!.calculateDriveRoute( - RouteSearchV2.DriveRouteQuery( + mRouteSearch!!.calculateDriveRouteAsyn( + RouteSearch.DriveRouteQuery( fromAndTo, - RouteSearchV2.DrivingStrategy.DEFAULT, + RouteSearch.DRIVEING_PLAN_FASTEST_SHORTEST, throughPointList.map { r -> r.convertToLatLonPoint() }, null, null ) ) - onDriveRouteSearched(driveRouteResult) } catch (e: AMapException) { dissmissProgressDialog() showerror(context, e.errorCode) @@ -216,10 +218,9 @@ class DrivingRoute(context: Context) : RouteOverlay(context) { } - private fun onDriveRouteSearched(result: DriveRouteResultV2) { - dissmissProgressDialog() + private fun onDriveRouteSearched(result: DriveRouteResult?) { map.clear() - if (result.paths != null) { + if (result?.paths != null) { if (result.paths.size > 0) { drivePath = result.paths[0] ?: return remove() @@ -255,4 +256,21 @@ class DrivingRoute(context: Context) : RouteOverlay(context) { searchRouteResult() } + override fun onDriveRouteSearched(result: DriveRouteResult?, errorCode: Int) { + dissmissProgressDialog() + if (errorCode == AMapException.CODE_AMAP_SUCCESS) { + onDriveRouteSearched(result) + } else { + showerror(context, errorCode) + } + } + + override fun onBusRouteSearched(result: BusRouteResult?, errorCode: Int) { + } + + override fun onWalkRouteSearched(result: WalkRouteResult?, errorCode: Int) { + } + + override fun onRideRouteSearched(result: RideRouteResult?, errorCode: Int) { + } } \ No newline at end of file diff --git a/lib/android/src/main/java/cn/feewee/amap3d/map_view/route/DrivingRouteManager.kt b/lib/android/src/main/java/cn/feewee/amap3d/map_view/route/DrivingRouteManager.kt index 111e910..7656212 100644 --- a/lib/android/src/main/java/cn/feewee/amap3d/map_view/route/DrivingRouteManager.kt +++ b/lib/android/src/main/java/cn/feewee/amap3d/map_view/route/DrivingRouteManager.kt @@ -85,7 +85,7 @@ class DrivingRouteManager : SimpleViewManager() { @ReactProp(name = "throughPointIcon") fun setThroughPointIcon(route: DrivingRoute, throughPoint: ReadableMap?) { - throughPoint?.let { route.setThroughPointBitDes(it) } + throughPoint?.let { route.setThroughBit(it) } } @ReactProp(name = "roadLine") diff --git a/lib/android/src/main/java/cn/feewee/amap3d/map_view/route/RouteOverlay.kt b/lib/android/src/main/java/cn/feewee/amap3d/map_view/route/RouteOverlay.kt index e2e0668..d70da63 100644 --- a/lib/android/src/main/java/cn/feewee/amap3d/map_view/route/RouteOverlay.kt +++ b/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 private var roadColor: Int = Color.parseColor("#537edc") private var progDialog: AlertDialog? = null - protected var mRouteSearch: RouteSearchV2? = null + protected var mRouteSearch: RouteSearch? = null abstract fun searchRouteResult() @@ -263,10 +263,10 @@ abstract class RouteOverlay(context: Context) : ReactViewGroup(context), Overlay if (startPoint != null) { try { val bounds = getLatLngBounds() - map.animateCamera( - CameraUpdateFactory - .newLatLngBounds(bounds, 100) - ) + val newLatLngBounds = CameraUpdateFactory + .newLatLngBounds(bounds, 100) + + map.animateCamera(newLatLngBounds) } catch (e: Throwable) { e.printStackTrace() } @@ -304,7 +304,7 @@ abstract class RouteOverlay(context: Context) : ReactViewGroup(context), Overlay /** * 开始搜索路径规划方案 */ - protected fun createFromAndTo(): RouteSearchV2.FromAndTo? { + protected fun createFromAndTo(): RouteSearch.FromAndTo? { if (startPoint == null) { show(context, "起点未设置") return null @@ -313,7 +313,7 @@ abstract class RouteOverlay(context: Context) : ReactViewGroup(context), Overlay show(context, "终点未设置") return null } - return RouteSearchV2.FromAndTo( + return RouteSearch.FromAndTo( startPoint!!.convertToLatLonPoint(), endPoint!!.convertToLatLonPoint() ) @@ -331,7 +331,7 @@ abstract class RouteOverlay(context: Context) : ReactViewGroup(context), Overlay */ protected fun showProgressDialog() { if (progDialog == null) { - progressBarCircleDialog() + progDialog = progressBarCircleDialog() } progDialog?.setCanceledOnTouchOutside(false); progDialog?.setCancelable(false); @@ -339,17 +339,17 @@ abstract class RouteOverlay(context: Context) : ReactViewGroup(context), Overlay } - private fun progressBarCircleDialog() { + private fun progressBarCircleDialog(): AlertDialog { val builder = AlertDialog.Builder( context, AlertDialog.THEME_HOLO_LIGHT ) - val inflater = LayoutInflater.from(context); + val inflater = LayoutInflater.from(context.applicationContext); @SuppressLint("InflateParams") val view: View = inflater.inflate(R.layout.dialog_progressbar_circle, null); val tv: TextView = view.findViewById(R.id.tv); tv.text = "正在搜索..."; builder.setView(view); - progDialog = builder.create(); + return builder.create(); } } \ No newline at end of file diff --git a/lib/android/src/main/res/layout/dialog_progressbar_circle.xml b/lib/android/src/main/res/layout/dialog_progressbar_circle.xml index 6accd5f..5ad3519 100644 --- a/lib/android/src/main/res/layout/dialog_progressbar_circle.xml +++ b/lib/android/src/main/res/layout/dialog_progressbar_circle.xml @@ -15,14 +15,13 @@ android:layout_margin="5dp" android:indeterminateDuration="300" /> - - diff --git a/lib/src/marker.tsx b/lib/src/marker.tsx index bfc5983..c356e13 100644 --- a/lib/src/marker.tsx +++ b/lib/src/marker.tsx @@ -1,11 +1,5 @@ import * as React from "react"; -import { - ImageSourcePropType, - NativeSyntheticEvent, - requireNativeComponent, - View, - ViewStyle, -} from "react-native"; +import { ImageSourcePropType, NativeSyntheticEvent, requireNativeComponent, View, ViewStyle } from "react-native"; // @ts-ignore import resolveAssetSource from "react-native/Libraries/Image/resolveAssetSource"; import Component from "./component"; @@ -14,8 +8,8 @@ import { LatLng, Point } from "./types"; export interface MarkerProps { /** * 坐标 - */ position: LatLng; - + */ + position: LatLng; /** * 图标 */ @@ -110,7 +104,7 @@ export default class extends Component { render() { const props = { ...this.props }; // @ts-ignore android 不能用 position 作为属性,会发生冲突 - props.latLng = props.position + props.latLng = props.position; // @ts-ignore delete props.position; if (props.children) { diff --git a/lib/src/polyline.tsx b/lib/src/polyline.tsx index 57053af..a733c31 100644 --- a/lib/src/polyline.tsx +++ b/lib/src/polyline.tsx @@ -54,7 +54,7 @@ export interface PolylineProps { } export default class extends React.PureComponent { - static defaultProps = { colors: [], with: 14 }; + static defaultProps = { colors: [], with: 16 }; render() { const props = { diff --git a/lib/src/route/driving-route.tsx b/lib/src/route/driving-route.tsx index 2b16b11..b2a1386 100644 --- a/lib/src/route/driving-route.tsx +++ b/lib/src/route/driving-route.tsx @@ -35,10 +35,6 @@ export interface DrivingRouteProps { */ roadLine?: ImageSourcePropType; /** - * 驾车图标 - */ - driveIcon?: ImageSourcePropType; - /** * 起点图标 */ startIcon?: ImageSourcePropType; @@ -57,12 +53,13 @@ export interface DrivingRouteProps { } export default class extends Component { - static defaultProps = { with: 14, throughPointVisible: true }; + name = name; + static defaultProps = { with: 16, throughPointVisible: true }; /** * 路线规划 */ - routePlan(start: LatLng, end: LatLng, throughPointList: LatLng[] = []) { + public routePlan(start: LatLng, end: LatLng, throughPointList: LatLng[] = []) { this.invoke("searchRoute", [start, end, throughPointList]); } @@ -73,7 +70,6 @@ export default class extends Component { return ( { } } -const NativeDrivingRoute = requireNativeComponent("DrivingRoute"); +const name = "DrivingRoute"; +const NativeDrivingRoute = requireNativeComponent(name); diff --git a/package.json b/package.json index 4f26380..e2827f4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cqfw/react-native-amap3d", - "version": "0.1.2", + "version": "0.1.4", "description": "react-native 高德地图组件,支持 Android + iOS", "author": "feewee", "license": "MIT", -- libgit2 0.22.2