| | |
| | | import android.os.Looper |
| | | import android.view.View |
| | | import androidx.core.content.ContextCompat |
| | | import com.amap.api.services.core.LatLonPoint |
| | | import com.amap.api.services.route.* |
| | | import com.amap.api.services.route.RouteSearch.DriveRouteQuery |
| | | import com.amap.api.services.route.RouteSearch.FromAndTo |
| | | import com.baidu.location.BDAbstractLocationListener |
| | | import com.baidu.location.LocationClient |
| | | import com.baidu.location.LocationClientOption |
| | |
| | | return (num / 3600).toString() + ":" + ((num % 3600) / 60).toString() + ":" + (((num % 3600) % 60) % 60).toString() + "s" |
| | | } |
| | | return "" |
| | | } |
| | | |
| | | fun initRouteLine( |
| | | context: Context, |
| | | start: LatLng, end: LatLng, |
| | | onClick: (latLngs: MutableList<LatLng>, lineTance: Float, lineTime: Long) -> Unit |
| | | ) { |
| | | initRouteLine(context, start, end, null, onClick) |
| | | } |
| | | |
| | | fun initRouteLine( |
| | | context: Context, |
| | | start: LatLng, end: LatLng, centerPoint: List<LatLonPoint>?, |
| | | onClick: (latLngs: MutableList<LatLng>, lineTance: Float, lineTime: Long) -> Unit |
| | | ) { |
| | | |
| | | |
| | | |
| | | var routeSearch = RouteSearch(context) |
| | | val fromAndTo = FromAndTo( |
| | | LatLonPoint(start.latitude, start.longitude), |
| | | LatLonPoint(end.latitude, end.longitude) |
| | | ) |
| | | val query = |
| | | DriveRouteQuery( |
| | | fromAndTo, |
| | | RouteSearch.DRIVING_SINGLE_SAVE_MONEY, |
| | | centerPoint, |
| | | null, |
| | | "" |
| | | ) |
| | | routeSearch.calculateDriveRouteAsyn(query) |
| | | routeSearch.setRouteSearchListener(object : RouteSearch.OnRouteSearchListener { |
| | | override fun onDriveRouteSearched(p0: DriveRouteResult?, p1: Int) { |
| | | if (p1 == 1000) { //获取规划路线成功,获取到的是了,路线坐标点的集合 |
| | | val paths: List<DrivePath> = p0!!.paths |
| | | var allDistance = 0f |
| | | if (paths.isNotEmpty()) { |
| | | val drivePath = paths[0] |
| | | //创建存储坐标点的集合 |
| | | val latLngs: MutableList<LatLng> = |
| | | ArrayList() |
| | | //遍历获取规划的所有路线坐标点 |
| | | for (mDriveStep in drivePath.steps) { |
| | | allDistance += mDriveStep.distance |
| | | for (mLatLonPoint in mDriveStep.polyline) { |
| | | latLngs.add( |
| | | LatLng( |
| | | mLatLonPoint.latitude, |
| | | mLatLonPoint.longitude |
| | | ) |
| | | ) |
| | | } |
| | | } |
| | | val lineTance = allDistance |
| | | val lineTime = (drivePath.duration / 60) //lineTime 分钟 |
| | | Handler(Looper.getMainLooper()).post { |
| | | onClick(latLngs, lineTance, lineTime) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | override fun onBusRouteSearched(p0: BusRouteResult?, p1: Int) { |
| | | } |
| | | |
| | | override fun onRideRouteSearched(p0: RideRouteResult?, p1: Int) { |
| | | } |
| | | |
| | | override fun onWalkRouteSearched(p0: WalkRouteResult?, p1: Int) { |
| | | } |
| | | |
| | | }) |
| | | } |
| | | |
| | | /*** |