先上图:
移动地图的视角下面的列表的数据可以自动改变,在顶部进行搜索会出现第二张图的列表。
代码是 Kotlin 写的,如果没学过的话读起来可能会有点麻烦……
第一个 Activity 的代码
class ShareLocationActivity : BaseActivity() {
override fun getLayoutRes(): Int = R.layout.activity_share_location
override fun initClick() {
restoreLocationButton.onClick {
restoreLocation()
}
shareLocationSearchButton.onClick {
searchPoi()
}
}
private fun searchPoi() {
var input = shareLocationEdit.text.toString()
if (input.isEmpty()) {
toast("请输入要搜索的内容")
return
}
launchActivity {
putExtra("keyword", input)
}
}
private var amapLocalUtils: AMapLocationUtils? = null
private fun restoreLocation() {
amapLocalUtils?.getLonLat(applicationContext) {
setCameraToCurrent(it.latitude, it.longitude)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initHead()
initMap(savedInstanceState)
// 定位
initPOI()
initMapCameraChangeListen()
initEvent()
initSearch()
initStateLayout()
}
private fun initStateLayout() {
shareLocationStateLayout.setErrorAndEmptyAction {
initPOI()
}
}
private fun initSearch() {
shareLocationEdit.setOnEditorActionListener { _, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_SEND
|| actionId == EditorInfo.IME_ACTION_DONE
|| (event != null && KeyEvent.KEYCODE_ENTER == event.keyCode &&
KeyEvent.ACTION_DOWN == event.action)) {
searchPoi()
true
}
false
}
}
private fun initEvent() {
Bus.observe()
.subscribe {
if (!isFinishing) {
finish()
Logcat.d("finish", "")
}
}
.registerInBus(this)
}
private fun initMapCameraChangeListen() {
getMap().setOnCameraChangeListener(object : AMap.OnCameraChangeListener {
override fun onCameraChangeFinish(position: CameraPosition) {
var target = position.target
decodePosition(target.latitude, target.longitude)
doPoiSearch(target.latitude, target.longitude)
}
override fun onCameraChange(position: CameraPosition?) {
}
})
}
private fun decodePosition(latitude: Double, longitude: Double) {
var search = GeocodeSearch(this)
search.setOnGeocodeSearchListener(object : GeocodeSearch.OnGeocodeSearchListener {
override fun onRegeocodeSearched(result: RegeocodeResult, code: Int) {
if (code != 200) {
shareLocationCityNameText.text = result.regeocodeAddress.city
cityCode = result.regeocodeAddress.cityCode
}
}
override fun onGeocodeSearched(result: GeocodeResult, code: Int) {
}
})
var query = RegeocodeQuery(LatLonPoint(latitude, longitude), 200f, GeocodeSearch.AMAP)
search.getFromLocationAsyn(query)
}
private var cityCode: String = ""
private fun initPOI() {
shareLocationStateLayout.showProgressView()
amapLocalUtils = AMapLocationUtils().get()
amapLocalUtils?.getLonLat(this) { location ->
cityCode = location.cityCode
doPoiSearch(location.latitude, location.longitude)
shareLocationCityNameText.text = location.city
}
}
private fun doPoiSearch(latitude: Double, longitude: Double) {
var query = PoiSearch.Query("", "地名地址信息", cityCode)
query.pageSize = 20
var poiSearch = PoiSearch(this, query)
poiSearch.bound = PoiSearch.SearchBound(LatLonPoint(latitude, longitude), 500000000)
poiSearch.setOnPoiSearchListener(object : PoiSearch.OnPoiSearchListener {
override fun onPoiItemSearched(item: PoiItem, code: Int) {
}
override fun onPoiSearched(result: PoiResult, code: Int) {
if (result.pois.isEmpty()) {
toast("附近没有标志性建筑")
}
Logcat.d(result.toString())
initListView(result.pois)
shareLocationStateLayout.showContentView()
}
})
poiSearch.searchPOIAsyn()
}
private fun initListView(pois: ArrayList) {
var adapter = ShareLocationAdapter(this, pois)
shareLocationListView.adapter = adapter
shareLocationListView.onItemClick { _, _, i, _ ->
var poi = pois[i]
Bus.send(ShareLocationEvent(poi.title, poi.latLonPoint.latitude, poi.latLonPoint.longitude))
finish()
Logcat.d("finish", "")
}
}
private fun setCameraToCurrent(latitude: Double, longitude: Double) {
var map = getMap()
map.animateCamera(CameraUpdateFactory
.newLatLngZoom(LatLng(latitude, longitude), 15f))
}
private fun getMap() = shareLocationMapView.map
private fun initMap(savedInstanceState: Bundle?) {
shareLocationMapView.onCreate(savedInstanceState)
var map = shareLocationMapView.map
initMyLocationDot(map)
}
private fun initMyLocationDot(map: AMap) {
map.isMyLocationEnabled = true
var locationStyle = MyLocationStyle()
locationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE)
locationStyle.interval(2000)
locationStyle.showMyLocation(true)
map.setMyLocationStyle(locationStyle)
}
private fun initHead() {
setToolbarTitle("新增地址")
setToolbarLeftButtonVisiable(true)
}
override fun onDestroy() {
super.onDestroy()
amapLocalUtils?.unRegister()
}
}
布局
第二个 Activity 的代码
class PoiSearchResultListActivity : BaseActivity() {
override fun getLayoutRes(): Int = R.layout.activity_poi_search_result_list
override fun initClick() {
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initHead()
initData()
}
private fun initData() {
showDialog()
var keyword = intent.getStringExtra("keyword")
AMapLocationUtils().getLonLat(this) {
location ->
doSearchPoiByKeyword(location, keyword)
}
}
private fun doSearchPoiByKeyword(location: AMapLocation, keyword: String) {
var query = PoiSearch.Query(keyword, "", location.cityCode)
query.pageSize = 50
var poiSearch = PoiSearch(this, query)
poiSearch.bound = PoiSearch.SearchBound(LatLonPoint(location.latitude, location.longitude), 5000000)
poiSearch.setOnPoiSearchListener(object : PoiSearch.OnPoiSearchListener {
override fun onPoiItemSearched(item: PoiItem, code: Int) {
}
override fun onPoiSearched(result: PoiResult, code: Int) {
if (result.pois.isEmpty()) {
toast("附近没有标志性建筑")
}
dismissDialog()
initListView(result.pois)
dismissDialog()
}
})
poiSearch.searchPOIAsyn()
}
private fun initListView(pois: ArrayList) {
poiSearchResultListView.adapter = ShareLocationAdapter(this, pois)
poiSearchResultListView.onItemClick{
_, _: View, i: Int, _: Long ->
var poi = pois[i]
Bus.send(ShareLocationEvent(poi.title, poi.latLonPoint.latitude, poi.latLonPoint.longitude))
finish()
Logcat.d("finish", "")
Bus.send(FinishShareLocationEvent())
}
}
private fun initHead() {
setToolbarTitle("选择地址")
setToolbarLeftButtonVisiable(true)
}
}
布局
代码和公司项目的耦合太多,不好分享出整个项目源码出来。有问题再问我。