美文网首页
高德地图 Android 模仿微信选择地址

高德地图 Android 模仿微信选择地址

作者: Obadiah | 来源:发表于2018-07-09 16:56 被阅读0次

先上图:


Screenshot_20180709-164828.jpg Screenshot_20180709-164840.jpg

移动地图的视角下面的列表的数据可以自动改变,在顶部进行搜索会出现第二张图的列表。

代码是 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<PoiSearchResultListActivity> {
            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<FinishShareLocationEvent>()
                .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<PoiItem>) {
        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()
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="cn.qingyangkeji.qingyang.activity.ShareLocationActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <include layout="@layout/layout_head"></include>

        <com.lufficc.stateLayout.StateLayout
            android:layout_width="match_parent"
            android:id="@+id/shareLocationStateLayout"
            android:layout_height="match_parent">
            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:background="@color/white"
                    android:orientation="horizontal"
                    android:gravity="center_vertical"
                    android:layout_height="73px">

                    <TextView
                        android:id="@+id/shareLocationCityNameText"
                        android:layout_width="wrap_content"
                        android:drawableLeft="@drawable/location_icon"
                        android:drawablePadding="15px"
                        android:text=""
                        android:layout_marginLeft="29px"
                        android:textColor="#2D2D2D"
                        android:textSize="30px"
                        android:layout_height="wrap_content" />

                    <ImageView
                        android:layout_width="18px"
                        android:src="@drawable/location_bottom_arrow"
                        android:layout_marginLeft="8px"
                        android:layout_height="12px" />

                    <EditText
                        android:maxLines="1"
                        android:singleLine="true"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:background="@null"
                        android:hint="查找周边学校 / 大厦 / 小区"
                        android:textColorHint="#999999"
                        android:textSize="26px"
                        android:imeOptions="actionDone"
                        android:id="@+id/shareLocationEdit"
                        android:layout_marginLeft="96px"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:paddingRight="20px"
                        android:paddingLeft="20px"
                        android:textColor="#2D2D2D"
                        android:id="@+id/shareLocationSearchButton"
                        android:layout_width="wrap_content"
                        android:text="搜索"
                        android:layout_height="wrap_content" />

                </LinearLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="647px">

                    <com.amap.api.maps2d.MapView
                        android:layout_width="match_parent"
                        android:id="@+id/shareLocationMapView"
                        android:layout_height="647px"></com.amap.api.maps2d.MapView>

                    <ImageView
                        android:layout_width="70px"
                        android:src="@drawable/restore_location_icon"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentLeft="true"
                        android:layout_marginLeft="15px"
                        android:id="@+id/restoreLocationButton"
                        android:layout_marginBottom="50px"
                        android:layout_height="70px" />

                    <ImageView
                        android:layout_width="48px"
                        android:id="@+id/shareLocationCenterIcon"
                        android:src="@drawable/location_center_icon"
                        android:layout_centerInParent="true"
                        android:layout_height="62px" />

                </RelativeLayout>

                <ListView
                    android:layout_width="match_parent"
                    android:id="@+id/shareLocationListView"
                    android:layout_height="match_parent"></ListView>


            </LinearLayout>

        </com.lufficc.stateLayout.StateLayout>



    </LinearLayout>

</RelativeLayout>

第二个 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<PoiItem>) {
        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)
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="cn.qingyangkeji.qingyang.activity.PoiSearchResultListActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <include layout="@layout/layout_head"></include>

        <ListView
            android:layout_width="match_parent"
            android:id="@+id/poiSearchResultListView"
            android:layout_height="match_parent"></ListView>

    </LinearLayout>

</RelativeLayout>

代码和公司项目的耦合太多,不好分享出整个项目源码出来。有问题再问我。

相关文章

网友评论

      本文标题:高德地图 Android 模仿微信选择地址

      本文链接:https://www.haomeiwen.com/subject/hwstpftx.html