lmw
2025-01-14 4645bee267f9c2a24c0e855a7907b2327557a90e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package com.xianning.driver.base.gaode
 
import android.content.Context
import android.location.LocationManager
import android.os.Bundle
import android.os.Handler
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
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.*
 
 
object AMapKit {
 
    fun initLocation(context: Context, lisener: BDAbstractLocationListener):LocationClient? { //初始化定位
        var mLocationClient:LocationClient? = null
        try {
            mLocationClient = LocationClient(context)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
        val option = LocationClientOption()
        option.locationMode = LocationMode.Hight_Accuracy
        option.setScanSpan(5000)
        //可选,设置发起定位请求的间隔,int类型,单位ms
        //如果设置为0,则代表单次定位,即仅定位一次,默认为0
        //如果设置非0,需设置1000ms以上才有效
        option.setOpenGnss(true)
        //可选,设置是否使用卫星定位,默认false
        //使用高精度和仅用设备两种定位模式的,参数必须设置为true
        option.setIgnoreKillProcess(true);
        //可选,定位SDK内部是一个service,并放到了独立进程。
        //设置是否在stop的时候杀死这个进程,默认(建议)不杀死,即setIgnoreKillProcess(true)
        option.SetIgnoreCacheException(false);
        option.setIsNeedAddress(true)
        //可选,设置是否收集Crash信息,默认收集,即参数为false
        mLocationClient?.setLocOption(option)
        mLocationClient?.registerLocationListener(lisener)
        mLocationClient?.start()
 
        return mLocationClient
    }
 
    fun initMap(savedInstanceState: Bundle?, mapView: MapView): BaiduMap {
        val aMap = mapView.map
        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): 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)
    }
 
 
    fun drawLineAndMove(context: Context, aMap: BaiduMap, start: LatLng, end: LatLng, padd: Int) {
        drawLine(context, aMap, start, end)
        moveCamera(aMap, start, end, padd)
    }
 
 
    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 折线对象
        return aMap.addOverlay(mOverlayOptions)
    }
 
    fun moveCamera(aMap: BaiduMap, latlng: LatLng) {
        val mapStatusUpdate = MapStatusUpdateFactory.newLatLng(latlng)
        aMap.animateMapStatus(mapStatusUpdate,16)
    }
 
    fun moveCamera(aMap: BaiduMap, start: LatLng?, end: LatLng?) {
        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?) {
        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.Builder().include(start).include(end).build()
        val mapStatusUpdate = MapStatusUpdateFactory.newLatLngBounds(bounds,padd,padd,padd,padd)
        aMap.animateMapStatus(mapStatusUpdate)
    }
 
    fun getOption(
        latLng: LatLng,
        view: View
    ): MarkerOptions {
        return MarkerOptions().anchor(0.5f, 1f)
            .position(latLng)
            .icon(BitmapDescriptorFactory.fromBitmap(BaseMapActivity.convertViewToBitmap(view)))
            .draggable(true).flat(true)
    }
 
    /***
     * 修改marker的view
     */
    fun getIcon(view: View): BitmapDescriptor {
        return BitmapDescriptorFactory.fromBitmap(BaseMapActivity.convertViewToBitmap(view))
    }
 
    /**
     * 添加marker点
     */
    fun addMarker(
        aMap: BaiduMap,
        mLat: Double?,
        mLng: Double?,
        view: View,
        peopleId: String
    ): Overlay? {
        if (mLat == null || mLng == null){
            return null
        }
        val options: MarkerOptions = getOption(LatLng(mLat, mLng), view)
        val marker: Overlay = aMap.addOverlay(options)
        return marker
    }
 
    fun getTance(tance: Float): String {
        if (tance == 0f) {
            return "0米"
        }
        return if (tance > 1000) {
            String.format("%.1f公里", (tance / 1000f))
        } else {
            String.format("%.1f米", tance)
        }
    }
 
 
 
    fun getTimeType(num: Int): String {
        if (num in 0..60) {
            return num.toString() + "s"
        }
        if (num in 60..60 * 60) {
            return (num / 60).toString() + ":" + (num % 60).toString() + "s"
        }
        if (num in 60..60 * 60 * 24) {
            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) {
            }
 
        })
    }
 
    /***
     * 是否开启gps
     */
    fun isLocServiceEnable(context: Context): Boolean {
        val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        var gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
        var network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
        if (gps || network) {
            return true
        }
        return false
    }
    }