lmw
2024-05-21 0af0750101f969b6ff18d7ade37354b4bcdccd03
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
package com.future.dispatch.ui.order.add_order
 
import android.content.Context
import android.os.Bundle
import com.amap.api.fence.GeoFence
import com.amap.api.fence.GeoFenceListener
import com.amap.api.maps.AMap
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Marker
import com.amap.api.services.core.LatLonPoint
import com.amap.api.services.core.PoiItem
import com.amap.api.services.geocoder.GeocodeResult
import com.amap.api.services.geocoder.GeocodeSearch
import com.amap.api.services.geocoder.RegeocodeQuery
import com.amap.api.services.geocoder.RegeocodeResult
import com.amap.api.services.poisearch.PoiResult
import com.amap.api.services.poisearch.PoiSearch
import com.example.oktrip.netUtls.callNet
import com.example.oktrip.netUtls.createView
import com.future.dispatch.R
import com.future.dispatch.base.BaseEvent
import com.future.dispatch.base.MyBaseActivity
import com.future.dispatch.base.gaode.AMapKit
import com.future.dispatch.bean.DistrictQueryBean
import com.future.dispatch.bean.QueryPositionBean
import com.future.dispatch.netUtls.Api
import com.future.dispatch.utils.clickDelay
import com.future.dispatch.utils.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: AMap? = 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() {
        AMapKit.addMapMoveListener(map!!, marker!!) { position ->
            AMapKit.searchPoint(
                this,
                position.latitude,
                position.longitude,
                object : PoiSearch.OnPoiSearchListener {
                    override fun onPoiItemSearched(p0: PoiItem?, p1: Int) {
 
                    }
 
                    override fun onPoiSearched(p0: PoiResult?, p1: Int) {
                        if (p0?.pois!!.size > 0) {
                            tv_city_name.text =  p0.pois[0].cityName
                            currentPosition = LatLng(
                                p0.pois[0].latLonPoint.latitude,
                                p0.pois[0].latLonPoint.longitude
                            )
                            if(!isSelectFirst){
                                tv_site.text = p0.pois[0].adName
                                tv_site_detail.text = p0.pois[0].title
                            }else{
                                isSelectFirst = false
                            }
                            currentSiteStr = tv_site.text.toString() + tv_site_detail.text.toString()
                            dismissDialog()
                        }
                    }
 
                })
        }
    }
 
    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(contentStr)
                } 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(str: String) {
        AMapKit.drawFenceByCity(this, str, GeoFenceListener { p0, p1, p2 ->
            if (p1 == GeoFence.ADDGEOFENCE_SUCCESS) {
                for (item in p0) {
                    for (itemP in item.pointList) {
                        AMapKit.drawFace(this, map!!, AMapKit.LatByDpoint(itemP))
                        if (itemP.size > 0){
                            addMk(LatLng(itemP[0].latitude,itemP[0].longitude))
                            AMapKit.moveCamera(map!!,LatLng(itemP[0].latitude,itemP[0].longitude))
                        }
                    }
                }
            }
        })
    }
 
    fun addFirstMarket() {
    }
 
    fun addMk(lat:LatLng){
        if (marker == null){
            var view = createView(R.layout.item_map_point, this)
            marker = AMapKit.addMarker(map!!, lat.latitude, lat.longitude, view, "12")
            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) {
        var geocoderSearch = GeocodeSearch(context);
        // 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
        var query =  RegeocodeQuery(LatLonPoint(lat,lon), 500f, GeocodeSearch.AMAP);
        geocoderSearch.getFromLocationAsyn(query);
        geocoderSearch.setOnGeocodeSearchListener(object : GeocodeSearch.OnGeocodeSearchListener {
            override fun onRegeocodeSearched(p0: RegeocodeResult?, p1: Int) {
                p0?.let {
                    if (p1 != 1000){
                        function("")
                    }else{
                        function(it.regeocodeAddress.city)
                    }
                }
            }
 
            override fun onGeocodeSearched(p0: GeocodeResult?, p1: Int) {
 
            }
 
        })
    }
 
 
    override fun setOnclick() {
        iv_reset_map.clickDelay {
            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
 
}