package com.casic.br.utils

import android.content.Context
import android.util.Log
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption

object LocationHelper {
    private const val kTag = "LocationHelper"

    fun obtainCurrentLocation(context: Context, listener: ILocationListener) {
        val locationClient = AMapLocationClient(context)
        val locationOption = AMapLocationClientOption()
        //设置定位模式为高精度模式，AMapLocationMode.Battery_Saving为低功耗模式，AMapLocationMode.Device_Sensors是仅设备模式
        locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
        locationOption.isOnceLocation = true
        locationClient.setLocationOption(locationOption)
        locationClient.setLocationListener {
            if (it.errorCode == 0) {
                listener.onAMapLocationGet(it)
            } else {
                listener.onAMapLocationGet(null)
                //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                Log.e(
                    kTag, "location Error, ErrCode: ${it.errorCode} , errInfo: ${it.errorInfo}"
                )
            }
        }
        locationClient.startLocation()
    }

    interface ILocationListener {
        fun onAMapLocationGet(aMapLocation: AMapLocation?) //高德定位数据
    }
}