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())
|
}
|
}
|
}
|
}
|
}
|