lmw
2025-04-14 0361f47762f9958f2ec91fdb62bfc98de4e162a6
app/src/main/java/com/xianning/driver/base/gaode/AMapKit.kt
@@ -1,28 +1,19 @@
package com.xianning.driver.base.gaode
import android.content.Context
import android.graphics.Color
import android.location.LocationManager
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.view.View
import androidx.core.content.ContextCompat
import cn.sinata.xldutils.utils.sysErr
import com.amap.api.fence.GeoFenceClient
import com.amap.api.fence.GeoFenceListener
import com.amap.api.services.core.LatLonPoint
import com.amap.api.services.geocoder.*
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
import com.baidu.location.LocationClientOption.LocationMode
import com.baidu.mapapi.map.*
import com.baidu.mapapi.model.LatLng
import com.baidu.mapapi.model.LatLngBounds
import com.xianning.driver.R
import java.util.*
@@ -50,30 +41,36 @@
        //设置是否在stop的时候杀死这个进程,默认(建议)不杀死,即setIgnoreKillProcess(true)
        option.SetIgnoreCacheException(false);
        option.setIsNeedAddress(true)
        //可选,默认gcj02,设置返回的定位结果坐标系,如果配合百度地图使用,建议设置为bd09ll;
        option.setCoorType("bd09ll")
        //可选,设置是否收集Crash信息,默认收集,即参数为false
        mLocationClient?.setLocOption(option)
        mLocationClient?.registerLocationListener(lisener)
        mLocationClient?.start()
        return mLocationClient
    }
    fun initMap(savedInstanceState: Bundle?, mapView: MapView): BaiduMap {
        val aMap = mapView.map
        val mMyLocationStyle = BaiduMapOptions()
        val uiSettings = aMap.uiSettings
        uiSettings.isCompassEnabled = false
        mapView.showScaleControl(false)
        mapView.showZoomControls(false)
        return aMap
    }
    fun drawLine(context: Context, aMap: BaiduMap, start: LatLng, end: LatLng): Polyline? {
//        val latLngs: MutableList<LatLng> = ArrayList()
//        latLngs.add(start)
//        latLngs.add(end)
//        return aMap.addPolyline(
//            PolylineOptions().addAll(latLngs).width(10f).color(
//                ContextCompat.getColor(context, R.color.main_yellow_qia)
//            )
//        )
        return null
    fun drawLine(context: Context, aMap: BaiduMap, start: LatLng, end: LatLng): Overlay? {
        val latLngs: MutableList<LatLng> = ArrayList()
        latLngs.add(start)
        latLngs.add(end)
        //设置折线的属性
        val mOverlayOptions: OverlayOptions = PolylineOptions()
            .width(10)
            .color(ContextCompat.getColor(context, R.color.main_yellow_qia))
            .points(latLngs)
        //在地图上绘制折线
        //mPloyline 折线对象
        return aMap.addOverlay(mOverlayOptions)
    }
@@ -83,61 +80,43 @@
    }
    fun drawLine(context: Context, aMap: BaiduMap, latLngs: List<LatLng?>?): Polyline? {
//        val options = PolylineOptions().addAll(latLngs).width(20f)
//            .color(ContextCompat.getColor(context, R.color.qing))
//        return aMap.addPolyline(options)
    fun drawLine(context: Context, aMap: BaiduMap, points: List<LatLng>?): Overlay? {
        val latLngs: MutableList<LatLng> = ArrayList()
        latLngs.addAll(points?: arrayListOf())
        //设置折线的属性
        val mOverlayOptions: OverlayOptions = PolylineOptions()
            .width(10)
            .color(ContextCompat.getColor(context, R.color.qing))
            .points(latLngs)
        //在地图上绘制折线
        //mPloyline 折线对象
        if (aMap!=null)
            return aMap.addOverlay(mOverlayOptions)
        return null
    }
    fun moveCamera(aMap: BaiduMap, latlng: LatLng?) {
//        val mCameraUpdate = CameraUpdateFactory.newCameraPosition(
//            CameraPosition(latlng, 16f, 0f, 0f)
//        )
//        aMap.animateCamera(mCameraUpdate)
    fun moveCamera(aMap: BaiduMap, latlng: LatLng) {
        val mapStatusUpdate = MapStatusUpdateFactory.newLatLng(latlng)
        aMap.animateMapStatus(mapStatusUpdate,8)
    }
    fun moveCamera(aMap: BaiduMap, start: LatLng?, end: LatLng?) {
//        var bounds: LatLngBounds? = null
//        if (start!!.latitude < end!!.latitude)
//            bounds = LatLngBounds(start, end)
//        else
//            bounds = LatLngBounds(end, start)
//        val mCameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 150)
//        aMap.animateCamera(mCameraUpdate, object : AMap.CancelableCallback {
//            override fun onFinish() {
//                sysErr("onFinish")
//            }
//
//            override fun onCancel() {
//                sysErr("onCancel")
//            }
//        })
        val bounds = LatLngBounds.Builder().include(start).include(end).build()
        val mapStatusUpdate = MapStatusUpdateFactory.newLatLngBounds(bounds,100,100,100,100)
        aMap.animateMapStatus(mapStatusUpdate)
    }
    fun moveCamera3(aMap: BaiduMap, start: LatLng?, end: LatLng?, car: LatLng?) {
//        var bounds: LatLngBounds? = null
//        if (start!!.latitude < end!!.latitude)
//            bounds = LatLngBounds(start, end)
//        else
//            bounds = LatLngBounds(end, start)
//        bounds.including(car)
//        val mCameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 150)
//        aMap.animateCamera(mCameraUpdate, object : AMap.CancelableCallback {
//            override fun onFinish() {
//                sysErr("onFinish")
//            }
//
//            override fun onCancel() {
//                sysErr("onCancel")
//            }
//        })
        val bounds = LatLngBounds.Builder().include(start).include(end).include(car).build()
        val mapStatusUpdate = MapStatusUpdateFactory.newLatLngBounds(bounds,150,150,150,150)
        aMap.animateMapStatus(mapStatusUpdate)
    }
    fun moveCamera(aMap: BaiduMap, start: LatLng?, end: LatLng?, padd: Int) {
//        val bounds = LatLngBounds(start, end)
//        val mCameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padd)
//        aMap.animateCamera(mCameraUpdate)
    fun moveCamera(aMap: BaiduMap, start: LatLng, end: LatLng, padd: Int) {
        val bounds = LatLngBounds.Builder().include(start).include(end).build()
        val mapStatusUpdate = MapStatusUpdateFactory.newLatLngBounds(bounds,padd,padd,padd,padd)
        aMap.animateMapStatus(mapStatusUpdate)
    }
    fun getOption(
@@ -166,13 +145,13 @@
        mLng: Double?,
        view: View,
        peopleId: String
    ): Marker? {
    ): Overlay? {
        if (mLat == null || mLng == null){
            return null
        }
        val options: MarkerOptions = getOption(LatLng(mLat, mLng), view)
        val marker: Overlay = aMap.addOverlay(options)
        return null
        return marker
    }
    fun getTance(tance: Float): String {
@@ -199,76 +178,6 @@
            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) {
            }
        })
    }
    /***