lmw
2025-08-18 9f6a5f46e6dfa59885ece3aad3a197de26a67b8c
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package com.xianning.driver.ui.main.add_order
 
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import cn.sinata.xldutils.utils.clickDelay
import com.baidu.mapapi.map.*
import com.baidu.mapapi.model.LatLng
import com.baidu.mapapi.model.LatLngBounds
import com.baidu.mapapi.search.core.SearchResult
import com.baidu.mapapi.search.district.OnGetDistricSearchResultListener
import com.baidu.mapapi.search.geocode.*
import com.baidu.mapapi.search.poi.*
import com.xianning.driver.R
import com.xianning.driver.base.BaseEvent
import com.xianning.driver.base.MyBaseActivity
import com.xianning.driver.base.gaode.AMapKit
import com.xianning.driver.bean.DistrictQueryBean
import com.xianning.driver.bean.QueryPositionBean
import com.xianning.driver.netUtls.Api
import com.xianning.driver.netUtls.callNet
import com.xianning.driver.netUtls.createView
import com.xianning.driver.netUtls.getMapByAny
import kotlinx.android.synthetic.main.activity_select_start_point.*
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
 
 
/**
 * @ClassName SelectStartPointActivity
 * @Description 选择起点 选择终点
 * @Author Administrator
 * @Date 2020/10/8 16:00
 * @Version 1.0
 */
class SelectStartPointActivity : MyBaseActivity() {
 
    val endCityId by lazy {
        intent.getStringExtra("endCityId")
    }
 
    val startCityId by lazy {
        intent.getStringExtra("startCityId")
    }
 
    val name by lazy {
        intent.getStringExtra("name")
    }
 
 
    var map: BaiduMap? = null
 
    var marker: Marker? = null
 
    var currentPosition: LatLng? = null
 
    var currentSiteStr: String = ""
 
    override fun setContentView() {
        setContentView(R.layout.activity_select_start_point)
    }
 
    override fun initView() {
        if (!startCityId.isNullOrEmpty()) {
            setTitleText("选择起点")
        } else {
            setTitleText("选择终点")
        }
        tv_city_name.text = ""
        callDistrict()
 
        tv_sure.clickDelay {
            if (currentPosition == null){
                toast("加载中请稍等...")
                return@clickDelay
            }
            AMapKit.getAddress(
                this,
                currentPosition!!.latitude,
                currentPosition!!.longitude
            ) { adcode ->
                if (adcode.isNullOrEmpty()){
                    toast("查询失败,请重试")
                    return@getAddress
                }
                var map = getMapByAny()
                map["code"] = adcode
                map["lonLat"] = currentPosition!!.longitude.toString() + "," + currentPosition!!.latitude.toString()
                map["siteId"] = if (startCityId.isNullOrEmpty()) endCityId else startCityId
                callNet(Api.areaMonitoring, map) {
                    var bean  = gson.fromJson<QueryPositionBean>(it, QueryPositionBean::class.java)
                    if (bean.data == -1){
                        toast("您选择的地址不在服务范围内")
                        return@callNet
                    }else{
                        currentPosition?.let {
                            EventBus.getDefault().post(
                                BaseEvent(
                                    BaseEvent.SELECT_POINT_SITE_DETAIL,
                                    currentSiteStr,
                                    currentPosition!!.latitude.toString(),
                                    it.longitude.toString()
                                )
                            )
                            onBackPressed()
                        }
                    }
 
                }
            }
        }
    }
 
    private fun moveLisener() {
        Log.e("ljqxs", "121:==")
        AMapKit.addMapMoveListener(map!!, marker!!) { position ->
//            AMapKit.getAddressReg(this,position.latitude,position.longitude){
//                if (!it.aois.isNullOrEmpty()){
//                    currentPosition = position
//                    if(!isSelectFirst){
//                        tv_site.text = it.district
//                        tv_site_detail.text = it.aois[0].aoiName
//                    }else{
//                        isSelectFirst = false
//                    }
//                    currentSiteStr = tv_site.text.toString() + tv_site_detail.text.toString()
//                    dismissDialog()
//                }
//            }
 
            Log.e("ljqxs", "MoveListener:position${position.latitude},${position.longitude}")
 
            AMapKit.searchPoint(
                this,
                position.latitude,
                position.longitude,
                object : OnGetPoiSearchResultListener {
                    override fun onGetPoiResult(p0: PoiResult?) {
                        Log.e("ljqxs", "154:结果${p0},${p0?.allPoi?.size}")
                        p0?.apply {
                            if (!allPoi.isNullOrEmpty()) {
                                tv_city_name.text = allPoi[0].city
                                currentPosition = LatLng(
                                    allPoi[0].location.latitude,
                                    allPoi[0].location.longitude
                                )
                                if (!isSelectFirst) {
                                    Log.e("ljqxs", "162:结果${allPoi[0].address}")
                                    tv_site.text = allPoi[0].name
                                    tv_site_detail.text = allPoi[0].address
                                } else {
                                    isSelectFirst = false
                                }
                                currentSiteStr =
                                    tv_site.text.toString() + tv_site_detail.text.toString()
                                dismissDialog()
                            }
                        }
                    }
 
                    override fun onGetPoiDetailResult(p0: PoiDetailResult?) {
                        Log.e("ljqxs", "172:onGetPoiDetailResult${p0}")
                    }
 
                    override fun onGetPoiDetailResult(p0: PoiDetailSearchResult?) {
                        Log.e("ljqxs", "172:onGetPoiDetailResult${p0}")
                    }
 
                    override fun onGetPoiIndoorResult(p0: PoiIndoorResult?) {
                        Log.e("ljqxs", "172:onGetPoiIndoorResult${p0}")
                    }
 
                })
        }
    }
 
    private fun callDistrict() {
        var map = getMapByAny()
        map["siteId"] = if (startCityId.isNullOrEmpty()) endCityId else startCityId
        callNet(Api.queryLocation, map) {
            var bean = gson.fromJson<DistrictQueryBean>(it, DistrictQueryBean::class.java)
            for (item in bean.data) {
                if (item.type == 1) { //    integer($int32)  区域类型(1=行政区域,2=电子围栏)
                    var contentStr = ""
                    if (item.province.isNotEmpty()) {
                        contentStr = item.province
                    }
                    if (item.city.isNotEmpty()) {
                        contentStr = item.city
                    }
                    if (item.district.isNotEmpty()) {
                        contentStr = item.district
                    }
                    searchName(item.city, item.district)
                } else {
                    addPointList(item.coordinate)
                }
            }
        }
    }
 
    private fun addPointList(coordinate: String) {
        var latAll = coordinate.split(";")
        if (latAll.size > 2){
            var allLat = mutableListOf<LatLng>()
            for (item in latAll) {
                var point = item.split(",")
                if (point.size == 2) {
                    var lat = LatLng(point[1].toDouble(), point[0].toDouble())
                    allLat.add(lat)
                }
            }
            AMapKit.drawFace(this, map!!, allLat)
            if (allLat.size > 0){
                addMk(LatLng(allLat[0].latitude, allLat[0].longitude))
                marker?.let {
                    it.position = LatLng(allLat[0].latitude, allLat[0].longitude)
                    AMapKit.moveCamera(map!!, it.position)
                }
            }
//            AMapKit.drawFenceByPoint(this, allPoint, GeoFenceListener { p0, p1, p2 ->
//                if (p1 == GeoFence.ADDGEOFENCE_SUCCESS) {
//                    for (item in p0) {
//                        for (itemP in item.pointList) {
//                        }
//                    }
//                }
//            })
        }
        if (latAll.size == 2){
           var point = latAll[0].split(",")
           var dpoint = LatLng(point[1].toDouble(), point[0].toDouble())
            AMapKit.drawCricle(this, map!!, dpoint, latAll[1].toDouble())
            AMapKit.moveCamera(map!!, dpoint)
            addMk(LatLng(dpoint.latitude, dpoint.longitude))
            marker?.let {
                it.position = LatLng(dpoint.latitude, dpoint.longitude)
                AMapKit.moveCamera(map!!, it.position)
            }
        }
 
    }
 
    fun searchName(city: String?, district: String?) {
        AMapKit.drawFenceByCity(
            this,
            city,
            district,
            OnGetDistricSearchResultListener { districtResult ->
                    //获取边界坐标点,并展示
                        val polyLines = districtResult.getPolylines()
                        if (polyLines != null) {
                            val builder = LatLngBounds.Builder()
                            for (polyline in polyLines!!) {
                                val ooPolyline11: OverlayOptions = PolylineOptions().width(1)
                                    .points(polyline).dottedLine(true).color(Color.RED)
                                map?.addOverlay(ooPolyline11)
                                val ooPolygon: OverlayOptions = PolygonOptions().points(polyline)
                                    .stroke(Stroke(1, Color.RED)).fillColor(0x1A000000)
                                map?.addOverlay(ooPolygon)
                                for (latLng in polyline) {
                                    builder.include(latLng)
                                }
                                if (polyline.size > 0) {
                                    Log.e(
                                        "ljqxs",
                                        "searchName:${city},结果${polyline[polyline.size / 2].latitude},${polyline[polyline.size / 2].longitude}"
                                    )
                                    addMk(
                                        LatLng(
                                            polyline[polyline.size / 2].latitude,
                                            polyline[polyline.size / 2].longitude
                                        )
                                    )
                                    AMapKit.moveCamera(
                                        map!!, LatLng(
                                            polyline[polyline.size / 2].latitude,
                                            polyline[polyline.size / 2].longitude
                                        )
                                    )
 
                                }
                            }
                            map?.setMapStatus(
                                MapStatusUpdateFactory
                                    .newLatLngBounds(builder.build())
                            )
                        }
            })
    }
 
    fun addMk(lat: LatLng){
        Log.e("ljqxs", "LatLng:结果${lat.latitude},${lat.longitude}")
        if (marker == null){
            var view = createView(R.layout.item_map_point, this)
            marker = AMapKit.addMarker(map!!, lat.latitude, lat.longitude, view, "12") as Marker
            currentPosition = marker?.position
            moveLisener()
            getAddress(this, currentPosition!!.latitude, currentPosition!!.longitude){
                tv_city_name.text = it
            }
        }
    }
 
    /***
     * 逆地址编码
     */
    fun getAddress(context: Context, lat: Double, lon: Double, function: (String) -> Unit) {
        val mCoder = GeoCoder.newInstance()
        val listener = object: OnGetGeoCoderResultListener {
            override fun onGetGeoCodeResult(p0: GeoCodeResult?) {
 
            }
 
            override fun onGetReverseGeoCodeResult(reverseGeoCodeResult: ReverseGeoCodeResult?) {
                if (reverseGeoCodeResult == null || reverseGeoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
                    //没有找到检索结果
                    function("")
                } else {
                    function(reverseGeoCodeResult.addressDetail.city)
                }
            }
 
        }
        mCoder.setOnGetGeoCodeResultListener(listener)
        mCoder.reverseGeoCode(
            ReverseGeoCodeOption()
                .location(LatLng(lat, lon)) // 设置是否返回新数据 默认值0不返回,1返回
                .newVersion(1) // POI召回半径,允许设置区间为0-1000米,超过1000米按1000米召回。默认值为1000
                .radius(500)
        )
    }
 
 
    override fun setOnclick() {
        iv_reset_map.clickDelay {
            if (currentPosition!=null)
                AMapKit.moveCamera(map!!, currentPosition!!)
        }
 
        tv_search.setOnClickListener {
            startActivity<SearchSiteActivity>(
                "currentCityName" to tv_city_name.text.toString(),
                "type" to if (!startCityId.isNullOrEmpty()) "1" else "2"
            )
        }
    }
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        map = AMapKit.initMap(savedInstanceState, map_view)
    }
 
    override fun onEventMainThread(event: BaseEvent?) {
        super.onEventMainThread(event)
        when (event!!.code) {
            BaseEvent.SELECT_SEARCH_SITE -> {
                isSelectFirst = true
                var list = event!!.msgthree.split(",")
                if (list.size == 2) {
                    tv_site.text = list[0]
                    tv_site_detail.text = list[1]
                }
                showDialog()
                AMapKit.moveCamera(map!!, LatLng(event.msg.toDouble(), event.msgTwo.toDouble()))
            }
        }
    }
 
    var  isSelectFirst = false
 
}