归志宁无五亩园,读书本意在元元。这篇文章主要讲述Android-閫氳繃GPS鎴栬€呯綉缁滆幏鍙栧綋鍓嶄綅缃?kotlin相关的知识,希望能为你提供帮助。
鏍囩锛?a href='http://www.mamicode.com/so/1/pack' title='pack'>pack
extc
network
location
otl
浠g爜
tail
lis
ant
鍦ˋndroidManifest涓坊鍔?/p>
< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> < uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
kotlin浠g爜
1private fun getLocation(context: Context): Location { 2val locMan = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager 3val checkCameraPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 4val checkCallPhonePermission = 5ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) 6if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED || checkCameraPermission != PackageManager.PERMISSION_GRANTED) { 7ActivityCompat.requestPermissions(this, permission, 2) 8} 9way.text = "閫氳繃GPS瀹氫綅" 10val location = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER) 11if (location == null) { 12way.text = "閫氳繃缃戠粶瀹氫綅" 13locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) 14} 15return location 16} 17 18private fun getGeoByLocation(location:Location){ 19longitude.text ="longitude锛?{location.longitude}" 20latitude.text = "latitude锛?{location.latitude}" 21val ge =Geocoder(this) 22var addressList =ArrayList< Address> () 23try { 24addressList = ge.getFromLocation(location.latitude,location.longitude,1) as ArrayList< Address> 25detail.text = addressList.toString() 26}catch (e:IOException){ 27e.printStackTrace() 28} 29if (addressList.size> 0){ 30address.text = "${addressList[0].getAddressLine(0)}" 31} 32}
鏈€鍚庣洿鎺ュ湪onCreate涓皟鐢ㄥ氨琛屼簡銆?/p>【Android-閫氳繃GPS鎴栬€呯綉缁滆幏鍙栧綋鍓嶄綅缃?kotlin】
杩欑鑾峰彇瀹氫綅鏂瑰紡涓嶉€傚悎闇€瑕佸疄鏃剁洃鍚綅缃彉鍖栫殑闇€姹傦紝鍙€傚悎鑾峰彇涓€娆°€?/p>
推荐阅读
- Android --------BouncingJellyView 果冻视图(阻尼效果)
- androidStudio&Intellij IDEA 通用快捷键
- Android 极光IM-高级篇-玩聊天app诞生
- Android的启动模式
- android sdk 没找到draw9patch.bat 如何制作.9.png 格式图片
- Caused by: java.lang.IllegalStateException: Ambiguous mapping found
- Linux Centos7 —sshd远程登录,密钥对登录,TCPWrappers访问控制
- PowerShell数组用法例子
- PowerShell算术运算符