18034727885
2026-07-06 a052061e479b4b2db89e66df01ef2a9a9eea3cf4
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
package com.fangxinxing.xxfxx.ui.main
 
import android.content.Context
import android.os.Bundle
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.MarkerOptions
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.fangxinxing.xxfxx.R
import com.fangxinxing.xxfxx.base.BaseEvent
import com.fangxinxing.xxfxx.base.MyApplication
import com.fangxinxing.xxfxx.base.MyBaseActivity
import com.fangxinxing.xxfxx.base.gaode.AMapKit
import com.fangxinxing.xxfxx.bean.AddressInfo
import com.fangxinxing.xxfxx.bean.DestinationInfo
import com.fangxinxing.xxfxx.utils.clickDelay
import kotlinx.android.synthetic.main.activity_set_destination.iv_reset_map
import kotlinx.android.synthetic.main.activity_set_destination.map_view
import kotlinx.android.synthetic.main.activity_set_destination.tv_city_name
import kotlinx.android.synthetic.main.activity_set_destination.tv_search
import kotlinx.android.synthetic.main.activity_set_destination.tv_site
import kotlinx.android.synthetic.main.activity_set_destination.tv_site_detail
import kotlinx.android.synthetic.main.activity_set_destination.tv_sure
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.startActivity
 
//设置目的地页
class SetDestinationActivity : MyBaseActivity() {
    //成都
    val cityName by lazy {
        intent.getStringExtra("cityName")
    }
 
    //经度
    val latitude by lazy {
        intent.getStringExtra("latitude")
    }
 
    //经度
    val longitude by lazy {
        intent.getStringExtra("longitude")
    }
 
    //标题
    val title by lazy {
        intent.getStringExtra("title")
    }
    var marker: Marker? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        map_view.onCreate(savedInstanceState)
    }
 
    override fun setContentView() {
        setContentView(R.layout.activity_set_destination)
    }
 
    override fun initView() {
        title?.also {
            setTitleText(it)
        }
        tv_city_name.text = ""
        initLocation()
    }
 
    //初始化位置
    private fun initLocation() {
        if (!cityName.isNullOrEmpty()) {
            searchByCityName(cityName)
            return
        }
        if (!latitude.isNullOrEmpty() && !longitude.isNullOrEmpty()) {
            initMarker(LatLng(latitude.toDoubleOrNull() ?: 0.0, longitude.toDoubleOrNull() ?: 0.0))
            return
        }
        MyApplication.aMapLocation?.also {
            initMarker(LatLng(it.latitude, it.longitude))
        }
    }
 
    //通过城市名搜索
    private fun searchByCityName(cityName: String) {
        map_view.map?.also { map ->
            AMapKit.searchByText(
                this,
                "${cityName}中心",
                cityName,
                object : PoiSearch.OnPoiSearchListener {
                    override fun onPoiSearched(p0: PoiResult?, p1: Int) {
                        p0?.pois?.firstOrNull()?.also {
                            val latLng = LatLng(it.latLonPoint.latitude, it.latLonPoint.longitude)
                            AMapKit.moveCamera(map, latLng) {
                                if (isFinishing || isDestroy) {
                                    return@moveCamera
                                }
                                initMarker(latLng)
                            }
                        }
                    }
 
                    override fun onPoiItemSearched(p0: PoiItem?, p1: Int) {
                    }
                })
        }
    }
 
    //初始化marker
    private fun initMarker(latLng: LatLng) {
        map_view.map?.also { map ->
            if (marker == null) {
                marker = map.addMarker(MarkerOptions().apply {
                    position(latLng)
                    icon(BitmapDescriptorFactory.fromResource(R.mipmap.zuobiao))
                    anchor(0.5f, 1f)
                })
            } else {
                marker?.position = latLng
            }
            moveListener()
            getAddress(this, latLng.latitude, latLng.longitude) {
                tv_city_name.text = it
            }
        }
    }
 
    //设置镜头移动监听
    private fun moveListener() {
        map_view.map?.also { map ->
            marker?.also {
                AMapKit.moveCamera(map, it.position)
                AMapKit.addMapMoveListener(map, it) { position ->
                    if (isFinishing || isDestroy) {
                        return@addMapMoveListener
                    }
                    searchPoint(position)
                }
            }
        }
    }
 
    //搜索坐标
    private fun searchPoint(position: LatLng) {
        AMapKit.searchAddress(
            this,
            position.latitude,
            position.longitude
        ) { lat, lng, regeocodeAddress ->
            setData(
                DestinationInfo(
                    lat,
                    lng,
                    regeocodeAddress.province,
                    regeocodeAddress.city,
                    regeocodeAddress.cityCode,
                    regeocodeAddress.district,
                    regeocodeAddress.formatAddress,
                    regeocodeAddress.adCode,
                )
            )
        }
    }
 
    //设置数据
    private fun setData(item: DestinationInfo) {
        tv_site.text = item.district
        tv_site_detail.text = item.address
        tv_city_name.text = item.city
        marker?.`object` = item
    }
 
    /***
     * 逆地址编码
     */
    private fun getAddress(context: Context, lat: Double, lon: Double, function: (String) -> Unit) {
        val geocoderSearch = GeocodeSearch(context);
        // 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
        val 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 {
            map_view.map?.also { map ->
                marker?.position?.also {
                    AMapKit.moveCamera(map, it)
                }
            }
        }
 
        tv_search.setOnClickListener {
            startActivity<SearchAddressActivity>("cityName" to tv_city_name.text.toString())
        }
        tv_sure.clickDelay {
            (marker?.`object` as? DestinationInfo)?.also {
                EventBus.getDefault().post(BaseEvent(BaseEvent.SELECT_ADDRESS, gson.toJson(it)))
            }
            finish()
        }
    }
 
    override fun onResume() {
        super.onResume()
        map_view.onResume()
    }
 
    override fun onPause() {
        super.onPause()
        map_view.onPause()
    }
 
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        map_view.onSaveInstanceState(outState)
    }
 
    override fun onDestroy() {
        super.onDestroy()
        map_view.onDestroy()
    }
 
    override fun onEventMainThread(event: BaseEvent?) {
        super.onEventMainThread(event)
        when (event!!.code) {
            BaseEvent.SELECT_SEARCH_ADDRESS -> {
                val addressInfo = gson.fromJson<AddressInfo>(event.msg, AddressInfo::class.java)
                map_view.map?.also { map ->
                    AMapKit.moveCamera(map, addressInfo.getLatLng())
                }
            }
        }
    }
}