100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > kotlin Android通过GPS定位/网络定位获取当前经纬度及位置信息

kotlin Android通过GPS定位/网络定位获取当前经纬度及位置信息

时间:2019-06-14 09:08:20

相关推荐

kotlin Android通过GPS定位/网络定位获取当前经纬度及位置信息

kotlin Android通过GPS定位/网络定位获取当前经纬度及位置信息

使用 kotlin 通过GPS定位/网络定位获取当前经纬度及位置信息

添加权限

android6.0后需要动态获取权限

Androidmanifest:

<!-- 这个权限用于进行网络定位 --><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><!-- 这个权限用于访问GPS定位 --><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

添加动态申请依赖:

//权限请求 支持Androidximplementation 'com.guolindev.permissionx:permissionx:1.6.4'

创建一个获取位置的工具类

class LocationUtilsprivate constructor(private val mContext: Context) {// 定位回调private var mLocationCallBack: LocationCallBack? = null// 定位管理实例var mLocationManager: LocationManager? = null/*** 获取定位* @param mLocationCallBack 定位回调* @return*/fun getLocation(mLocationCallBack: LocationCallBack?) {this.mLocationCallBack = mLocationCallBackif (mLocationCallBack == null) return// 定位管理初始化mLocationManager = mContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager// 通过GPS定位val gpsProvider = mLocationManager!!.getProvider(LocationManager.GPS_PROVIDER)// 通过网络定位val netProvider = mLocationManager!!.getProvider(WORK_PROVIDER)// 优先考虑GPS定位,其次网络定位。if (gpsProvider != null) {gpsLocation()} else if (netProvider != null) {netWorkLocation()} else {mLocationCallBack.setLocation(null)}}/*** GPS定位* @return*/@SuppressLint("MissingPermission")private fun gpsLocation() {if (mLocationManager == null) mLocationManager =mContext.getSystemService(Context.LOCATION_SERVICE) as LocationManagermLocationManager!!.requestLocationUpdates(GPS_LOCATION, MIN_TIME, MIN_DISTANCE, mLocationListener)}/*** 网络定位*/@SuppressLint("MissingPermission")private fun netWorkLocation() {if (mLocationManager == null) mLocationManager =mContext.getSystemService(Context.LOCATION_SERVICE) as LocationManagermLocationManager!!.requestLocationUpdates(NETWORK_LOCATION, MIN_TIME, MIN_DISTANCE, mLocationListener)}// 定位监听private val mLocationListener: LocationListener = object : LocationListener {override fun onLocationChanged(location: Location) {if (mLocationCallBack != null) {mLocationCallBack!!.setLocation(location)}}override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}override fun onProviderEnabled(provider: String) {}override fun onProviderDisabled(provider: String) {// 如果gps定位不可用,改用网络定位if (provider == LocationManager.GPS_PROVIDER) {netWorkLocation()}}}/*** 根据经纬度获取地址* @param latitude 纬度* @param longitude 经度*/fun getAddress(latitude: Double, longitude: Double) {// Address列表var locationList: List<Address?>? = null// 经纬度解码实例val gc = Geocoder(mContext, Locale.getDefault())try {// 获取Address列表locationList = gc.getFromLocation(latitude, longitude, MAX_RESULTS)// 获取Address实例val address = locationList[0]if (mLocationCallBack != null) mLocationCallBack!!.setAddress(address)} catch (e: IOException) {e.printStackTrace()}}/*** 获取地址周边信息* @param* @return*/fun getAddressLine(address: Address): String {var result = ""var i = 0while (address.getAddressLine(i) != null) {val addressLine = address.getAddressLine(i)result = result + addressLinei++}return result}/*** @className: LocationCallBack* @classDescription: 定位回调*/interface LocationCallBack {fun setLocation(location: Location?)fun setAddress(address: Address?)}companion object {// GPS定位private const val GPS_LOCATION = LocationManager.GPS_PROVIDER// 网络定位private const val NETWORK_LOCATION = WORK_PROVIDER// 解码经纬度最大结果数目private const val MAX_RESULTS = 1// 时间更新间隔,单位:msprivate const val MIN_TIME: Long = 1000// 位置刷新距离,单位:mprivate const val MIN_DISTANCE = 0.01.toFloat()// singletonprivate var instance: LocationUtils? = null/*** singleton* @param mContext 上下文* @return*/fun getInstance(mContext: Context): LocationUtils? {if (instance == null) {instance = LocationUtils(mContext)}return instance}}}

在Activity中动态申请并使用工具类:

PermissionX.init(this).permissions(Manifest.permission.ACCESS_FINE_LOCATION).request{allGranted, deniedList, _ ->if(allGranted){LocationUtils.getInstance(this)!!.getLocation(object : LocationUtils.LocationCallBack {override fun setLocation(location: Location?) {if (location != null) {longitude.text = "经度:" + location.longitude.toString() // longitude为控件idlatitude.text = "纬度:" + location.latitude.toString() // latitude为控件idLocationUtils.getInstance(this@MainActivity)!!.getAddress(location.latitude,location.longitude)}}override fun setAddress(address: Address?) {if (address != null) {tv_address.text = """国家:${address.countryName}城市名:${address.locality}周边信息:${LocationUtils.getInstance(this@MainActivity)!!.getAddressLine(address)}""".trimIndent()}}})}else{Toast.makeText(this," You denied ${deniedList}",Toast.LENGTH_SHORT).show()}}

这里后面在使用在平板电脑安装应用发现无法获取位置,发现通过GPS定位获取位置需要有电话卡的手机才能运行

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。