Android-閫氳繃GPS鎴栬€呯綉缁滆幏鍙栧綋鍓嶄綅缃?kotlin

归志宁无五亩园,读书本意在元元。这篇文章主要讲述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>



    推荐阅读