package cn.flightfeather.thirdappmodule.module.common
|
|
import android.annotation.SuppressLint
|
import android.content.Intent
|
import android.os.Bundle
|
import android.os.Parcelable
|
import android.view.View
|
import cn.flightfeather.thirdappmodule.R
|
import cn.flightfeather.thirdappmodule.module.base.BaseMapActivity
|
import com.amap.api.maps.AMap.OnCameraChangeListener
|
import com.amap.api.maps.CameraUpdateFactory
|
import com.amap.api.maps.MapView
|
import com.amap.api.maps.model.*
|
import com.amap.api.services.core.LatLonPoint
|
import com.amap.api.services.geocoder.GeocodeResult
|
import com.amap.api.services.geocoder.GeocodeSearch
|
import com.amap.api.services.geocoder.RegeocodeQuery
|
import com.amap.api.services.geocoder.RegeocodeResult
|
import kotlinx.android.parcel.Parcelize
|
import kotlinx.android.synthetic.main.activity_map_location.*
|
import kotlinx.android.synthetic.main.layout_button_back.*
|
|
/**
|
* @author riku
|
* Date: 2020/6/9
|
* 获取地图定位信息activity
|
*/
|
class MapLocationActivity : BaseMapActivity(), View.OnClickListener {
|
|
companion object {
|
const val RESULT_CODE_LOCATION = "location"
|
}
|
|
private lateinit var geocodeSearch: GeocodeSearch
|
|
private val location = Location()
|
|
private var firstMove = true
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
geocodeSearch = GeocodeSearch(this)
|
super.onCreate(savedInstanceState)
|
img_back.setOnClickListener(this)
|
txt_submit.setOnClickListener(this)
|
initLocation()
|
}
|
|
override fun onResume() {
|
super.onResume()
|
aMap.isMyLocationEnabled = true
|
}
|
|
override fun onPause() {
|
super.onPause()
|
aMap.isMyLocationEnabled = false
|
}
|
|
override fun onDestroy() {
|
super.onDestroy()
|
aMap.isMyLocationEnabled = false
|
}
|
|
override fun onMapInitStart() {
|
//初始化详细地址描述查询功能
|
geocodeSearch.setOnGeocodeSearchListener(object : GeocodeSearch.OnGeocodeSearchListener {
|
@SuppressLint("SetTextI18n")
|
override fun onRegeocodeSearched(p0: RegeocodeResult?, p1: Int) {
|
//成功
|
if (p1 == 1000) {
|
p0?.let {
|
it.regeocodeAddress.apply {
|
location.address = formatAddress
|
|
location.address = location.address.replace(township, "")
|
if (aois.isNotEmpty()) {
|
location.name = aois[0].aoiName
|
location.address = location.address.replace(location.name, "")
|
}
|
|
val part1 = if (province==city) province else province + city
|
part1+district
|
|
runOnUiThread {
|
txt_location_content.text = location.address
|
txt_latitude_content.text = "${location.latitude}, ${location.longitude}"
|
}
|
}
|
}
|
}
|
}
|
|
override fun onGeocodeSearched(p0: GeocodeResult?, p1: Int) {
|
p0?.geocodeQuery
|
}
|
})
|
}
|
|
override fun onMarkerClick(marker: Marker) = Unit
|
|
override fun getMapView(): MapView = mapView
|
|
override fun getLayoutId(): Int = R.layout.activity_map_location
|
|
override fun initMap() {
|
aMap.setRenderFps(60)
|
aMap.uiSettings.isZoomControlsEnabled = false
|
aMap.uiSettings.isScaleControlsEnabled = true
|
aMap.setOnCameraChangeListener(object : OnCameraChangeListener {
|
override fun onCameraChange(cameraPosition: CameraPosition) {
|
|
}
|
|
override fun onCameraChangeFinish(cameraPosition: CameraPosition) {
|
location.clear()
|
location.latitude = cameraPosition.target.latitude
|
location.longitude = cameraPosition.target.longitude
|
|
val query = RegeocodeQuery(LatLonPoint(location.latitude, location.longitude), 200F, GeocodeSearch.AMAP)
|
geocodeSearch.getFromLocationAsyn(query)
|
}
|
})
|
aMap.setOnMyLocationChangeListener {
|
if (firstMove) {
|
val desLatLng = LatLng(it.latitude, it.longitude)
|
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(desLatLng, 15f))
|
firstMove = false
|
}
|
}
|
aMap.setOnMapLoadedListener { addMarkerInScreenCenter() }
|
|
}
|
|
override fun initLocation() {
|
val myLocationStyle: MyLocationStyle = MyLocationStyle() //初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。
|
|
// myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。
|
// myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。
|
myLocationStyle.showMyLocation(true)
|
myLocationStyle.radiusFillColor(resources.getColor(R.color.transparent))
|
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER) //连续定位、蓝点不会移动到地图中心点,定位点依照设备方向旋转,并且蓝点会跟随设备移动
|
|
aMap.myLocationStyle = myLocationStyle //设置定位蓝点的Style
|
|
aMap.uiSettings.isMyLocationButtonEnabled = false //设置默认定位按钮是否显示,非必需设置。
|
|
aMap.isMyLocationEnabled = true // 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。
|
|
}
|
|
//在屏幕中心添加一个固定的marker
|
private fun addMarkerInScreenCenter() {
|
val latLng = aMap.cameraPosition.target
|
val screenPosition = aMap.projection.toScreenLocation(latLng)
|
val locationMarker = aMap.addMarker(MarkerOptions()
|
.anchor(0.5f, 0.5f)
|
.icon(BitmapDescriptorFactory.fromResource(R.drawable.purple_pin)))
|
//设置Marker在屏幕上,不跟随地图移动
|
locationMarker.setPositionByPixels(screenPosition.x, screenPosition.y)
|
locationMarker.zIndex = 1f
|
}
|
|
override fun onClick(v: View?) {
|
when (v?.id) {
|
R.id.img_back -> onBackPressed()
|
R.id.txt_submit -> {
|
val intent = Intent()
|
intent.putExtra(RESULT_CODE_LOCATION, location)
|
setResult(RESULT_OK, intent)
|
onBackPressed()
|
}
|
}
|
}
|
|
@Parcelize
|
data class Location(
|
var name: String = "",
|
var address: String = "",
|
var latitude: Double = 0.0,
|
var longitude: Double = 0.0
|
) : Parcelable {
|
fun clear() {
|
name = ""
|
address = ""
|
latitude = 0.0
|
longitude = 0.0
|
}
|
}
|
}
|