Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

จะรับที่อยู่ที่สมบูรณ์จากละติจูดและลองจิจูดบน Android Kotlin ได้อย่างไร


ตัวอย่างนี้สาธิตวิธีรับที่อยู่ที่สมบูรณ์จากละติจูดและลองจิจูดบน Android Kotlin

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml

  <ปุ่ม android:id="@+id/btnShowAddress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text ="แสดงที่อยู่" /> 

ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน src/MainActivity.kt

<ก่อนหน้า>นำเข้า android.content.Intentimport android.content.pm.PackageManagerimport android.location.Locationimport android.location.LocationManagerimport android.os.Bundleimport android.os.Handlerimport android.os.Messageimport android.provider.Settingsimport android.widget Buttonimport android.widget.TextViewimport android.widget.Toastimport androidx.appcompat.app.AlertDialogimport androidx.appcompat.app.AppCompatActivityimport androidx.core.app.ActivityCompatimport androidx.core.content.ContextCompatclass กิจกรรมหลัก:ปุ่ม AppCompatitActivity () { ล่าช้า lateinit var tvAddress:TextView lateinit ตำแหน่ง var:ตำแหน่ง lateinit var appLocationService:AppLocationService แทนที่ความสนุกบนสร้าง (savedInstanceState:Bundle?) { super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) title ="KotlinApp" tvAddress =findView.ByIddress (R.layout.activity_main) id.tvAddress) appLocationService =AppLocationService (นี้) btnShowAddress =findView ById (R.id.btnShowAddress) ถ้า (ContextCompat.checkSelfPermission (นี่ Manifest.permission.ACCESS_FINE_LOCATION) !=PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale (this, Manifest.permission.ACCESS_FINE_FINE) , arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 1); } อื่น ๆ { ActivityCompat.requestPermissions (นี่, arrayOf (Manifest.permission.ACCESS_FINE_LOCATION), 1); } } btnShowAddress.setOnClickListener { ตำแหน่ง =appLocationService.getLocation (LocationManager.GPS_PROVIDER) !! ละติจูดของวาล =13.1000727 ลองจิจูดของวาล =80.2126274 วาล locationAddress =LocationAddress () locationAddress.getAddressFromLocation (ละติจูด, ลองจิจูด, บริบทของแอปพลิเคชัน, GeoCodeHandler () ) showSettingsAlert () } } ความสนุกส่วนตัว showSettingsAlert () { val alertDialog =AlertDialog alert.Dlogia setTitle("SETTINGS") alertDialog.setMessage("Enable Location Provider! Go to settings menu?") alertDialog.setPositiveButton("Settings") { _, _ -> val Intent =Intent (Settings.ACTION_LOCATION_SOURCE_SETTINGS) this@MainActivity.startActivity (เจตนา) } alertDialog.setNegativeButton ("ยกเลิก") { กล่องโต้ตอบ _ -> dialog.cancel () } alertDialog.show () } GeoCodeHandler ระดับภายในภายใน:ตัวจัดการ () { แทนที่ fun handleMessage (ข้อความ:ข้อความ) { val locationAddress :สตริง locationAddress =when (message.what) { 1 -> { val bundle =mes sage.data bundle.getString("address") } else -> null.toString() } tvAddress.text =locationAddress } } แทนที่ความสนุก onRequestPermissionsResult ( requestCode:Int, การอนุญาต:Array, grantResults:IntArray ) { เมื่อ (requestCode) { 1 -> { if (grantResults.isNotEmpty() &&GrantResults[0] ===PackageManager.PERMISSION_GRANTED) { if ((ContextCompat.checkSelfPermission( this@MainActivity, Manifest.permission.ACCESS_FINE_LOCATION ) ===PackageManager. PERMISSION_GRANTED) ) { Toast.makeText (นี่คือ "การอนุญาตที่ได้รับ", Toast.LENGTH_SHORT).show() } } อื่น { Toast.makeText (นี้ "การอนุญาตถูกปฏิเสธ", Toast.LENGTH_SHORT).show () } ส่งคืน } } }}

ขั้นตอนที่ 4 − สร้างคลาส kotlin และเพิ่มรหัสต่อไปนี้ใน AppLocationService.kt

ตัวอย่าง

<ก่อนหน้า>นำเข้า android.annotation.SuppressLintimport android.app.Serviceimport android.content.Contextimport android.content.Intentimport android.location.Locationimport android.location.LocationListenerimport android.location.LocationManagerimport android.os.Bundleimport android.os.IBinderopen class AppLocationService (บริบท:บริบท) :บริการ (), LocationListener { ส่วนตัว var locationManager:LocationManager? =context.getSystemService(Context.LOCATION_SERVICE) เป็น LocationManager ตำแหน่ง lateinit ส่วนตัวของ LocationManager:ตำแหน่ง @SuppressLint ("MissingPermission") สนุก getLocation (ผู้ให้บริการ:สตริง?):ตำแหน่ง? { if (locationManager!!.isProviderEnabled (provider)) { locationManager!!.requestLocationUpdates (ผู้ให้บริการ MIN_TIME_FOR_UPDATE MIN_DISTANCE_FOR_UPDATE.toFloat() นี่ ) ถ้า (locationManager !=null) { location =locationManager!!.getLastKnownLocation (provider) location } } return null } แทนที่ fun onLocationChanged(location:Location) {} override fun onProviderDisabled(provider:String) {} override fun onProviderEnabled(provider:String) {} override fun onStatusChanged( provider:String, status:Int, extras:Bundle ) { } แทนที่ความสนุก onBind(arg0:Intent):IBinder? { คืนค่า null } วัตถุที่แสดงร่วม { ค่า const ส่วนตัว MIN_DISTANCE_FOR_UPDATE:Long =10 ค่า const ส่วนตัว MIN_TIME_FOR_UPDATE =1000 * 60 * 2.toLong() }}

ขั้นตอนที่ 5 − สร้างคลาส kotlin และเพิ่มรหัสต่อไปนี้ใน LocationAddress.kt

ตัวอย่าง

<ก่อนหน้า>นำเข้า android.content.Contextimport android.location.Geocoderimport android.os.Bundleimport android.os.Handlerimport android.os.Messageimport android.util.Logimport java.io.IOExceptionimport java.util.*class LocationAddress { แท็ก val ส่วนตัว ="LocationAddress" สนุก getAddressFromLocation ( ละติจูด:สองเท่า ลองจิจูด:สองเท่า บริบท:บริบท ตัวจัดการ:ตัวจัดการ ) { val thread =วัตถุ:เธรด () { แทนที่ fun run () { val geoCoder =Geocoder (บริบท Locale.getDefault ( ) ) ผลลัพธ์ var:String =null.toString() ลอง { val addressList =geoCoder.getFromLocation( ละติจูด ลองจิจูด 1 ) if ((addressList !=null &&addressList.size> 0)) { val address =addressList.get( 0) val sb =StringBuilder () สำหรับ (i ใน 0 จนถึง address.maxAddressLineIndex) { sb.append (address.getAddressLi ne(i)).append("\n") } sb.append(address.locality).append("\n") sb.append(address.postalCode).append("\n") sb.append( address.countryName) ผล =sb.toString() } } จับ (e:IOException) { Log.e (แท็ก "ไม่สามารถเชื่อมต่อกับ GeoCoder", e) } ในที่สุด { val message =Message.obtain () message.target =handler message.what =1 val บันเดิล =Bundle() result =("ละติจูด:" + ละติจูด + " ลองจิจูด:" + ลองจิจูด + "\n\nที่อยู่:\n" + ผล) bundle.putString("ที่อยู่" ผลลัพธ์ ) message.data =มัด message.sendToTarget() } } } thread.start() }}

ขั้นตอนที่ 6 − เพิ่มรหัสต่อไปนี้ใน androidManifest.xml

 <แอปพลิเคชัน android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android :roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">    <หมวดหมู่ android:name="android.intent.category.LAUNCHER" />   

มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์ของคุณแล้วคลิกไอคอน Run จะรับที่อยู่ที่สมบูรณ์จากละติจูดและลองจิจูดบน Android Kotlin ได้อย่างไร จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก จากนั้นตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ

จะรับที่อยู่ที่สมบูรณ์จากละติจูดและลองจิจูดบน Android Kotlin ได้อย่างไร