|
@@ -0,0 +1,289 @@
|
|
|
+package com.pan_american.android.ui.customer_resource.customer_visit
|
|
|
+
|
|
|
+import android.Manifest
|
|
|
+import android.graphics.Color
|
|
|
+import android.os.Build
|
|
|
+import android.os.Bundle
|
|
|
+import android.util.Log
|
|
|
+import com.amap.api.location.AMapLocation
|
|
|
+import com.amap.api.location.AMapLocationClient
|
|
|
+import com.amap.api.location.AMapLocationClientOption
|
|
|
+import com.amap.api.location.AMapLocationListener
|
|
|
+import com.amap.api.maps.AMap
|
|
|
+import com.amap.api.maps.LocationSource
|
|
|
+import com.amap.api.maps.LocationSource.OnLocationChangedListener
|
|
|
+import com.amap.api.maps.UiSettings
|
|
|
+import com.amap.api.maps.model.MyLocationStyle
|
|
|
+import com.pan_american.android.OASystem
|
|
|
+import com.pan_american.android.R
|
|
|
+import com.pan_american.android.base.BaseActivity
|
|
|
+import com.pan_american.android.databinding.ActivityCustomerDistributionMapBinding
|
|
|
+import com.pan_american.android.databinding.LayoutTitleBinding
|
|
|
+import pub.devrel.easypermissions.AfterPermissionGranted
|
|
|
+import pub.devrel.easypermissions.EasyPermissions
|
|
|
+
|
|
|
+class CustomerDistributionMap : BaseActivity<ActivityCustomerDistributionMapBinding>(), AMapLocationListener, LocationSource, EasyPermissions.PermissionCallbacks {
|
|
|
+
|
|
|
+ private lateinit var titleBinding: LayoutTitleBinding
|
|
|
+
|
|
|
+ private lateinit var locationClient: AMapLocationClient
|
|
|
+
|
|
|
+ private lateinit var clientOption: AMapLocationClientOption
|
|
|
+
|
|
|
+ private lateinit var aMap: AMap
|
|
|
+
|
|
|
+ private lateinit var locationChangedListener: OnLocationChangedListener
|
|
|
+
|
|
|
+ private val myLocationStyle = MyLocationStyle()
|
|
|
+
|
|
|
+ private lateinit var uiSettings: UiSettings
|
|
|
+
|
|
|
+ override fun getViewBinding() = ActivityCustomerDistributionMapBinding.inflate(layoutInflater)
|
|
|
+
|
|
|
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
+ super.onCreate(savedInstanceState)
|
|
|
+
|
|
|
+ binding.mapView.onCreate(savedInstanceState)
|
|
|
+
|
|
|
+ initTitle()
|
|
|
+
|
|
|
+ //初始化定位
|
|
|
+ initLocation()
|
|
|
+
|
|
|
+ //初始化地图
|
|
|
+ initMap(savedInstanceState)
|
|
|
+
|
|
|
+ //检查Android 版本
|
|
|
+ checkingAndroidVersion()
|
|
|
+
|
|
|
+ initEvents()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun initTitle() {
|
|
|
+ titleBinding = LayoutTitleBinding.bind(binding.root).apply {
|
|
|
+ titleText.text = resources.getString(R.string.customer_distribution_map)
|
|
|
+
|
|
|
+ backButton.setOnClickListener {
|
|
|
+ back()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun initViews() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun initEvents() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查Android版本
|
|
|
+ */
|
|
|
+ private fun checkingAndroidVersion() {
|
|
|
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
|
|
|
+ //Android6.0及以上先获取权限再定位
|
|
|
+ requestPermission()
|
|
|
+ }else {
|
|
|
+ //Android6.0以下直接定位
|
|
|
+ locationClient.startLocation()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 动态请求权限
|
|
|
+ */
|
|
|
+ @AfterPermissionGranted(OASystem.LOCATION_REQUEST_CODE)
|
|
|
+ private fun requestPermission() {
|
|
|
+ val permissions = arrayOf(
|
|
|
+ Manifest.permission.ACCESS_COARSE_LOCATION,
|
|
|
+ Manifest.permission.ACCESS_FINE_LOCATION,
|
|
|
+ Manifest.permission.READ_PHONE_STATE,
|
|
|
+ Manifest.permission.WRITE_EXTERNAL_STORAGE
|
|
|
+ )
|
|
|
+
|
|
|
+ if (EasyPermissions.hasPermissions(this, *permissions)) {
|
|
|
+ //true 有权限 开始定位
|
|
|
+ // 这里不需要调用 startLocation(),移到 onPermissionsGranted 中
|
|
|
+ // 停止之前的定位,为 onPermissionsGranted 中的定位做准备
|
|
|
+ locationClient.stopLocation()
|
|
|
+ } else {
|
|
|
+ //false 无权限
|
|
|
+ EasyPermissions.requestPermissions(this, "使用该界面需要您开启定位权限", OASystem.LOCATION_REQUEST_CODE, *permissions)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 请求权限结果
|
|
|
+ */
|
|
|
+ override fun onRequestPermissionsResult(
|
|
|
+ requestCode: Int,
|
|
|
+ permissions: Array<out String>,
|
|
|
+ grantResults: IntArray
|
|
|
+ ) {
|
|
|
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
|
+ //设置权限请求结果
|
|
|
+ EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
|
|
|
+ if (requestCode == OASystem.LOCATION_REQUEST_CODE) {
|
|
|
+ locationClient.stopLocation() // 停止之前的定位请求 (非常重要!)
|
|
|
+ clientOption.setOnceLocation(true) // 只定位一次
|
|
|
+ locationClient.setLocationOption(clientOption) // 重新设置 option
|
|
|
+ locationClient.startLocation() // 启动定位
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化定位
|
|
|
+ */
|
|
|
+ private fun initLocation() {
|
|
|
+ //初始化定位
|
|
|
+ try {
|
|
|
+ locationClient = AMapLocationClient(OASystem.context)
|
|
|
+ } catch (e: Exception) {
|
|
|
+ e.printStackTrace()
|
|
|
+ }
|
|
|
+
|
|
|
+ //设置定位回调监听
|
|
|
+ locationClient.setLocationListener(this)
|
|
|
+ //初始化AMapLocationClientOption对象
|
|
|
+ clientOption = AMapLocationClientOption()
|
|
|
+ //设置定位模式为AMapLocationMode.Height_Accuracy,高精度模式。
|
|
|
+ clientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy)
|
|
|
+ //获取最近3s内精度最高的一次定位结果:
|
|
|
+ //设置setOnceLocationLatest(boolean b)接口为true,启动定位时SDK会返回最近3s内精度最高的一次定位结果。
|
|
|
+ // 如果设置其为true,setOnceLocation(boolean b)接口也会被设置为true,反之不会,默认为false。
|
|
|
+ clientOption.setOnceLocationLatest(true)
|
|
|
+ //设置是否返回地址信息(默认返回地址信息)
|
|
|
+ clientOption.setNeedAddress(true)
|
|
|
+ //设置定位请求超时时间,单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。
|
|
|
+ clientOption.setHttpTimeOut(20000)
|
|
|
+ //关闭缓存机制,高精度定位会产生缓存。
|
|
|
+ clientOption.setLocationCacheEnable(false)
|
|
|
+ //给定位客户端对象设置定位参数
|
|
|
+ locationClient.setLocationOption(clientOption)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 接收异步返回的定位结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ override fun onLocationChanged(aMapLocation: AMapLocation) {
|
|
|
+ if (aMapLocation.errorCode == 0) {
|
|
|
+ //地址
|
|
|
+// val address = aMapLocation.address
|
|
|
+//
|
|
|
+// val latitude = aMapLocation.latitude
|
|
|
+//
|
|
|
+// val longitude = aMapLocation.longitude
|
|
|
+
|
|
|
+// Log.e("location", "纬度: $latitude\n经度: $longitude\n地址: $address")
|
|
|
+
|
|
|
+ //停止定位后,本地服务并不会被销毁
|
|
|
+ // 不需要手动停止定位了,因为已经设置为单次定位
|
|
|
+ //locationClient.stopLocation()
|
|
|
+
|
|
|
+ //显示地图定位结果
|
|
|
+ locationChangedListener.onLocationChanged(aMapLocation)
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
|
|
|
+ Log.e("AmapError", "location Error, ErrCode:"
|
|
|
+ + aMapLocation.errorCode + ", errInfo:"
|
|
|
+ + aMapLocation.errorInfo
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化地图
|
|
|
+ */
|
|
|
+ private fun initMap(savedInstanceState: Bundle?) {
|
|
|
+
|
|
|
+ //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图
|
|
|
+
|
|
|
+ binding.mapView.onCreate(savedInstanceState)
|
|
|
+
|
|
|
+ aMap = binding.mapView.map
|
|
|
+
|
|
|
+ //设置最小缩放级别
|
|
|
+ aMap.minZoomLevel = 3.0f
|
|
|
+
|
|
|
+ // 自定义精度范围的圆形边框颜色 都为0则透明
|
|
|
+ myLocationStyle.strokeColor(Color.argb(0, 0, 0, 0))
|
|
|
+
|
|
|
+ // 自定义精度范围的圆形边框宽度 0 无宽度
|
|
|
+ myLocationStyle.strokeWidth(0.0f)
|
|
|
+
|
|
|
+ // 设置圆形的填充颜色 都为0则透明
|
|
|
+ myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0))
|
|
|
+
|
|
|
+ //设置定位蓝点的Style
|
|
|
+ aMap.myLocationStyle = myLocationStyle
|
|
|
+
|
|
|
+ // 设置定位监听
|
|
|
+ aMap.setLocationSource(this)
|
|
|
+
|
|
|
+ // 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
|
|
|
+ aMap.isMyLocationEnabled = true
|
|
|
+
|
|
|
+ //实例化UiSettings类对象
|
|
|
+ uiSettings = aMap.uiSettings
|
|
|
+
|
|
|
+ //隐藏缩放按钮
|
|
|
+ uiSettings.isZoomControlsEnabled = false
|
|
|
+
|
|
|
+ //显示比例尺 默认不显示
|
|
|
+ uiSettings.isScaleControlsEnabled = true
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 激活定位
|
|
|
+ */
|
|
|
+ override fun activate(listener: OnLocationChangedListener) {
|
|
|
+ locationChangedListener = listener
|
|
|
+
|
|
|
+ locationClient.startLocation()
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 停止定位
|
|
|
+ */
|
|
|
+ override fun deactivate() {
|
|
|
+ locationClient.stopLocation()
|
|
|
+
|
|
|
+ locationClient.onDestroy()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onResume() {
|
|
|
+ super.onResume()
|
|
|
+
|
|
|
+ binding.mapView.onResume()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onPause() {
|
|
|
+ super.onPause()
|
|
|
+
|
|
|
+ binding.mapView.onPause()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onSaveInstanceState(outState: Bundle) {
|
|
|
+ super.onSaveInstanceState(outState)
|
|
|
+
|
|
|
+ binding.mapView.onSaveInstanceState(outState)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onDestroy() {
|
|
|
+ super.onDestroy()
|
|
|
+
|
|
|
+ locationClient.onDestroy()
|
|
|
+
|
|
|
+ binding.mapView.onDestroy()
|
|
|
+ }
|
|
|
+}
|