package com.fanghua.driver.ui.main.add_order
|
|
import android.content.Intent
|
import android.util.Log
|
import androidx.recyclerview.widget.LinearLayoutManager
|
import cn.sinata.xldutils.net.utils.ResultException
|
import cn.sinata.xldutils.utils.clickDelay
|
import cn.sinata.xldutils.utils.setTextChange
|
import com.amap.api.services.core.LatLonPoint
|
import com.amap.api.services.help.Inputtips
|
import com.amap.api.services.help.Tip
|
import com.amap.api.services.poisearch.PoiResult
|
import com.amap.api.services.poisearch.PoiSearch
|
|
import com.fanghua.driver.R
|
import com.fanghua.driver.base.BaseEvent
|
import com.fanghua.driver.base.MyApplication
|
import com.fanghua.driver.base.MyBaseActivity
|
import com.fanghua.driver.base.gaode.AMapKit
|
import com.fanghua.driver.ui.adapter.SearchSitedapter
|
import com.fanghua.driver.utils.Cache.CacheKey
|
import io.reactivex.Flowable
|
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.subscribers.DisposableSubscriber
|
import kotlinx.android.synthetic.main.activity_search_site.*
|
import org.greenrobot.eventbus.EventBus
|
|
/**
|
* @ClassName SearchSiteActivity
|
* @Description 搜索地点
|
* @Author Administrator
|
* @Date 2020/10/9 10:09
|
* @Version 1.0
|
*/
|
class SearchSiteActivity: MyBaseActivity() {
|
|
val siteSearchAdapter by lazy {
|
SearchSitedapter()
|
}
|
|
val type by lazy {
|
intent.getStringExtra("type") //1起点 2终点
|
}
|
|
private val LogTag = "SearchSiteActivity==========》"
|
|
override fun setContentView() {
|
setContentView(R.layout.activity_search_site)
|
}
|
|
override fun initView() {
|
setTitleText(if (type == "1") "选择起点" else {
|
tv_search.hint = "请输入终点"
|
"选择终点"
|
})
|
|
recycler_view_site.layoutManager = LinearLayoutManager(this)
|
recycler_view_site.adapter = siteSearchAdapter
|
getPoi()
|
}
|
|
override fun setOnclick() {
|
tv_search.setTextChange {
|
if (it.isNotEmpty()){
|
AMapKit.searchSite(this,it,MyApplication.aMapLocation?.city?:"",
|
Inputtips.InputtipsListener { p0, p1 ->
|
if (!p0.isNullOrEmpty()){
|
siteSearchAdapter.data.clear()
|
siteSearchAdapter.data.addAll(siteSearchAdapter.setDateSite(p0))
|
siteSearchAdapter.notifyDataSetChanged()
|
}
|
})
|
}
|
}
|
|
siteSearchAdapter.setOnItemClickListener { view, position ->
|
// CacheKey.saveSiteList(siteSearchAdapter.data[position],type) //不保存历史
|
val intent = Intent()
|
intent.putExtra("name",siteSearchAdapter.data[position].site)
|
.putExtra("lat",siteSearchAdapter.data[position].lat)
|
.putExtra("lon",siteSearchAdapter.data[position].lng)
|
setResult(RESULT_OK,intent)
|
finish()
|
}
|
|
tv_clear.clickDelay {
|
CacheKey.putKeyStr("searchSite","")
|
siteSearchAdapter.data.clear()
|
siteSearchAdapter.notifyDataSetChanged()
|
}
|
}
|
|
/**
|
* 获取poi信息
|
*/
|
private var poiSearchDisposable: DisposableSubscriber<PoiResult>? = null
|
fun getPoi() {
|
if (MyApplication.aMapLocation == null) {
|
return
|
}
|
//如果上一次还没处理完,取消订阅
|
if (poiSearchDisposable != null && !poiSearchDisposable!!.isDisposed) {
|
poiSearchDisposable?.dispose()
|
}
|
poiSearchDisposable = object : DisposableSubscriber<PoiResult>() {
|
override fun onComplete() {
|
|
}
|
|
override fun onNext(t: PoiResult?) {
|
val address = if (t != null) {
|
if (t.pois.isNotEmpty()) {
|
t.pois[0].title
|
} else {
|
null
|
}
|
} else {
|
null
|
}
|
if (t != null)
|
runOnUiThread {
|
t.pois.sortBy { it.distance }
|
siteSearchAdapter.data.addAll(t.pois.map {
|
val bean = SearchSitedapter.SiteBean()
|
bean.site = it.title
|
bean.siteDetail = it.snippet
|
bean.lat = it.latLonPoint.latitude
|
bean.lng = it.latLonPoint.longitude
|
bean
|
})
|
siteSearchAdapter.notifyDataSetChanged()
|
}
|
}
|
|
override fun onError(t: Throwable?) {
|
Log.e("LogTag","周边获取失败:${t?.message}")
|
}
|
}
|
val query = PoiSearch.Query("", "190000", "")
|
val poiSearch = PoiSearch(this, query)
|
poiSearch.bound = PoiSearch.SearchBound(LatLonPoint(MyApplication.aMapLocation!!.latitude, MyApplication.aMapLocation!!.longitude), 100)
|
Flowable.just(poiSearch).subscribeOn(Schedulers.io()).flatMap {
|
try {
|
val result = poiSearch.searchPOI()
|
if (result == null) {
|
Flowable.error(ResultException(""))
|
} else
|
Flowable.just(result)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
Flowable.error<PoiResult>(e)
|
}
|
}.subscribe(poiSearchDisposable)
|
}
|
}
|