百度地图选点定位界面

百度地图选点定位界面,第1张

百度地图选点定位界面 1.去申请app的Appkey,对百度sdk的引入和配置

2.xml


    

        
    
    
        

            

            
        




        

        

        

            

            

        

    

3Java代码

open class MapNavigation : baseActivity(), BaiduMap.OnMapClickListener, onGetGeoCoderResultListener {
    private lateinit var mBaiduMap: BaiduMap
    private var markList: ArrayList = ArrayList()
    private var mClusterManager: ClusterManager? = null
    private var zoomTo = 13.0f
    private var mMapStatus: MapStatus? = null
    private lateinit var mSearch : GeoCoder
    private var lat = 28.234893
    private var lng = 112.94547
    private var viewId = 0
    private var position = -1

    companion object {
        @JvmStatic
        fun goMapNavigationActivity(context: Context, lon: Double, lat: Double,viewId: Int,position: Int) {
            val intent = Intent(context, MapNavigation().javaClass)
            intent.putExtra("lon", lon)
            intent.putExtra("lat", lat)
            intent.putExtra("viewId", viewId)
            intent.putExtra("position", position)
            ActivityUtils.startActivity(intent)
        }
        @JvmStatic
        fun goMapNavigationActivity(context: Context) {
            ActivityUtils.startActivity(Intent(context, MapNavigation().javaClass))
        }

    }

    override fun init() {
        mBaiduMap = getBinding().bmapView.map
        //设置不让手势旋转地图
        mBaiduMap.uiSettings.isRotateGesturesEnabled = false;
        mBaiduMap.uiSettings.isOverlookingGesturesEnabled = false;
//         设置地图监听,当地图状态发生改变时,进行点聚合运算
        mBaiduMap.setonMapStatusChangeListener(mClusterManager)
//         设置maker点击时的响应
        mBaiduMap.setonMarkerClickListener(mClusterManager)
        mSearch = GeoCoder.newInstance()
        mSearch.setonGetGeoCodeResultListener(this)

        intent.getStringExtra("lat")
        lat = intent.getDoubleExtra("lat",28.234893)
        lng = intent.getDoubleExtra("lon",112.94547)
        viewId = intent.getIntExtra("viewId",0)
        position = intent.getIntExtra("position",-1)
        setMarks(lat,lng)

        getBinding().setListener {
            when (it.id) {
                R.id.tv_sure -> {
                    initLocation()

                    if(getBinding().tvLatlng.text.toString().isNotEmpty()){

                        EventBusUtil.post(MapSelectLocationEvent(
                            getBinding().tvLatlng.text.toString(),getBinding().tvAddress.text.toString(),
                            viewId,position))
                    }
                    finish()
                }
            }
        }
        mBaiduMap.setonMapClickListener(this)
        mBaiduMap.isMyLocationEnabled = true
        // 初始化定位
        //地图加载完成回调,(只在初始化调一次)
        mBaiduMap.setonMapLoadedCallback {
            mBaiduMap.setMaxAndMinZoomLevel(18.toFloat(), 11.toFloat())
            // 初始化默认定位点
            mMapStatus =
                MapStatus.Builder().target(LatLng(lat, lng)).zoom(zoomTo).build()
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(mMapStatus))
            getBinding().bmapView.setMapCustomStyleEnable(true)
        }
        // 隐藏百度logo
        val child: View = getBinding().bmapView.getChildAt(1)
        if (child != null && (child is ImageView || child is ZoomControls)) {
            child.visibility = View.GONE
        }
        initListener()
    }

    
    private fun setMarks(lat: Double, lon: Double) {
        mBaiduMap.clear()
        markList.clear()
        val markView =
            LayoutInflater.from(this).inflate(R.layout.layout_baidu_mark, null)
//        val markIndex = markView.findViewById(R.id.iv_mark)
//                        val split = it.color.replace("rgb(", "").replace(")", "").split(",")

        val bundle = Bundle()
        val dataInfo = MapMarkBean.Data()
        dataInfo.site = "$lon,$lat"

        bundle.putSerializable("datainfo", dataInfo)
        val markerOptionsA = MarkerOptions()
            .extraInfo(bundle)
            // 经纬度
            .position(LatLng(lat, lon))
            // 设置 Marker 覆盖物的图标
            .icon(BitmapDescriptorFactory.fromView(markView))
//            .icon(BitmapDescriptorFactory.fromResource(R.drawable.dwd))
            // 设置 marker 在地图的层级
            .zIndex(9)
            // 设置Marker是否可点击
            .clickable(false)
            // 设置 marker 是否允许拖拽,默认不可拖拽
            .draggable(false)
            // 设置是否开启 marker 覆盖物近大远小效果,默认开启
            .perspective(false)
            // 设置 marker 覆盖物的锚点比例,默认(0.5f, 1.0f)水平居中,垂直下对齐
            .anchor(0.5f, 1.0f)
            // 设置多少帧刷新一次图片资源,Marker动画的间隔时间,值越小动画越快 默认为20,最小为1
            .period(10)
//        // 掉下动画
//        markerOptionsA.animateType(MarkerOptions.MarkerAnimateType.drop)
        // 生长动画
//        markerOptionsC.animateType(MarkerOptions.MarkerAnimateType.grow)
        markList.add(markerOptionsA)
        mBaiduMap.addOverlay(markerOptionsA)
    }

    
    private fun initListener() {
        // 设置Marker 点击事件监听
        mBaiduMap.setonMarkerClickListener { marker ->
            val dataInfo = marker.extraInfo.getSerializable("datainfo") as MapMarkBean.Data
            dataInfo.let {
                val list = ArrayList()
                list.add(dataInfo)
//                showMarkInfo(list)
            }
            true
        }

        // 设置地图监听,当地图状态发生改变时,进行点聚合运算
        mBaiduMap.setonMapStatusChangeListener(object : BaiduMap.onMapStatusChangeListener {
            
            override fun onMapStatusChangeStart(p0: MapStatus?) {
                // 手势 *** 作地图,设置地图状态等 *** 作导致地图状态开始改变。
            }

            
            override fun onMapStatusChangeStart(p0: MapStatus?, p1: Int) {
                // 手势 *** 作地图,设置地图状态等 *** 作导致地图状态开始改变。
            }

            
            override fun onMapStatusChange(p0: MapStatus?) {
                // 地图状态变化中
            }

            
            override fun onMapStatusChangeFinish(p0: MapStatus?) {
                // 地图状态改变结束
                //   p0.zoom 地图缩放级别 4~21,室内图支持到22
                LogUtil.d("zoom->", p0?.zoom.toString())

            }

        })
    }

    override fun onResume() {
        super.onResume()
        // MapView的生命周期与Activity同步,当activity onResume 时必须调用MapView.onResume()
        getBinding().bmapView.onResume()

    }

    override fun onPause() {
        super.onPause()
        // MapView的生命周期与Activity同步,当activity销毁时必须调用MapView.onPause()
        getBinding().bmapView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        //不允许图层定位
        mBaiduMap.isMyLocationEnabled = false
        // 清除所有图层
        mBaiduMap.clear()
        mSearch.destroy()
        getBinding().bmapView.setMapCustomStyleEnable(false)
        // MapView的生命周期与Activity同步,当activity销毁时必须调用MapView.destroy()
        getBinding().bmapView.onDestroy()
    }

    override fun isUserEventBus(): Boolean {
        return false
    }

    override fun onMapClick(p0: LatLng?) {
        if (p0 != null) {
            LogUtils.d("onMapClick"+p0.latitude+"lon"+p0.longitude)
            setMarks(p0.latitude, p0.longitude)
            mSearch.reverseGeoCode(
                ReverseGeoCodeOption()
                    .location(p0)
            )
        }
    }

    override fun onMapPoiClick(p0: MapPoi?) {
        if (p0 != null) {
            LogUtils.d("onMapClick"+p0.position.latitude+"lon"+p0.position.longitude)
            setMarks(p0.position.latitude, p0.position.longitude)
            mSearch.reverseGeoCode(
                ReverseGeoCodeOption()
                    .location(p0.position)
            )
        }
    }

    //正向编码,通过地址获得经纬度
    override fun onGetGeoCodeResult(result: GeoCodeResult?) {
    }

    //反向编码,通过经纬度获得地址
    override fun onGetReverseGeoCodeResult(result: ReverseGeoCodeResult?) {

        if (result == null || result.error !== SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(this, "抱歉,未能找到结果", Toast.LENGTH_LONG)
                .show()

            getBinding().tvLatlng.text = "位置:"
            getBinding().tvAddress.text = "未获取到位置"
            return
        }
        if(getBinding().llLocation.visibility == View.GONE){
            getBinding().llLocation.visibility = View.VISIBLE
        }
//        getBinding().tvLatlng.text = "位置:"+result.location.longitude+","+result.location.latitude
//        getBinding().tvLatlng.text = ""+result.location.longitude+","+result.location.latitude
        getBinding().tvLatlng.text = ""+
                String.format("%.6f", result.location.longitude)+","+String.format("%.6f", result.location.latitude)
        getBinding().tvAddress.text = ""+result.address

    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5686844.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存