Android-閫氳繃GPS鎴栬€呯綉缁滆幏鍙栧綋鍓嶄綅缃?kotlin
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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爜
1 private fun getLocation(context: Context): Location { 2 val locMan = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager 3 val checkCameraPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 4 val checkCallPhonePermission = 5 ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) 6 if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED || checkCameraPermission != PackageManager.PERMISSION_GRANTED) { 7 ActivityCompat.requestPermissions(this, permission, 2) 8 } 9 way.text = "閫氳繃GPS瀹氫綅" 10 val location = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER) 11 if (location == null) { 12 way.text = "閫氳繃缃戠粶瀹氫綅" 13 locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) 14 } 15 return location 16 } 17 18 private fun getGeoByLocation(location:Location){ 19 longitude.text ="longitude锛?{location.longitude}" 20 latitude.text = "latitude锛?{location.latitude}" 21 val ge =Geocoder(this) 22 var addressList =ArrayList<Address>() 23 try { 24 addressList = ge.getFromLocation(location.latitude,location.longitude,1) as ArrayList<Address> 25 detail.text = addressList.toString() 26 }catch (e:IOException){ 27 e.printStackTrace() 28 } 29 if (addressList.size>0){ 30 address.text = "${addressList[0].getAddressLine(0)}" 31 } 32 }
鏈€鍚庣洿鎺ュ湪onCreate涓皟鐢ㄥ氨琛屼簡銆?/p>
杩欑鑾峰彇瀹氫綅鏂瑰紡涓嶉€傚悎闇€瑕佸疄鏃剁洃鍚綅缃彉鍖栫殑闇€姹傦紝鍙€傚悎鑾峰彇涓€娆°€?/p>
以上是关于Android-閫氳繃GPS鎴栬€呯綉缁滆幏鍙栧綋鍓嶄綅缃?kotlin的主要内容,如果未能解决你的问题,请参考以下文章