1、效果如下:
2、主要代码
//拖动停止的时候,获取中心经纬度
mapView.map.setOnMapStatusChangeListener(object : BaiduMap.OnMapStatusChangeListener {
override fun onMapStatusChangeStart(p0: MapStatus?) {
mapView.map.clear()
iv_map.isVisible = true
}
override fun onMapStatusChangeStart(p0: MapStatus?, p1: Int) {
}
override fun onMapStatusChange(p0: MapStatus?) {
}
override fun onMapStatusChangeFinish(p0: MapStatus?) {
p0?.apply {
mapView.map.clear()
iv_map.visibility = View.INVISIBLE
setMarker(p0.target)
}
}
})
//构建Marker图标
private fun setMarker(latLng: LatLng) {
ll_point.isVisible = true
tv_lat?.text = DecimalFormat("#.000000").format(latLng.latitude)
tv_lng?.text = DecimalFormat("#.000000").format(latLng.longitude)
//构建MarkerOption,用于在地图上添加Marker
val option: OverlayOptions = MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_map))
//在地图上添加Marker,并显示
mapView.map.addOverlay(option)
}