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

ฉันจะค้นหาละติจูดและลองจิจูดจากที่อยู่ใน Android โดยใช้ Kotlin ได้อย่างไร


ตัวอย่างนี้สาธิตวิธีค้นหาละติจูดและลองจิจูดจากที่อยู่ใน Android โดยใช้ Kotlin

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

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

   <ปุ่ม android:id="@+id/addressButton" android:layout_width="wrap_content" android:layou t_height="wrap_content" android:layout_below="@+id/textViewAddress" android:layout_marginTop="50dp" android:layout_toEndOf="@+id/textViewAddress" android:text="Show Lat/Long" /> 

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

<ก่อนหน้า>นำเข้า android.os.Bundleimport android.os.Handlerimport android.os.Messageimport android.widget.Buttonimport android.widget.EditTextimport android.widget.TextViewimport androidx.appcompat.app.AppCompatActivityclass MainActivity:AppCompatActivity () { lateinit ส่วนตัว addressButton:ปุ่ม lateinit ส่วนตัว var textViewAddress:TextView lateinit var textViewLatLong:TextView แทนที่ fun onCreate (savedInstanceState:Bundle?) { super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) title ="KotlinApp" textViewAddress =findView.ByI .textViewAddress) textViewLatLong =findViewById (R.id.latLongTV) addressButton =ที่อยู่ findViewById (R.id.addressButton) ปุ่ม.setOnClickListener { val editText =findViewById(R.id.editTextAddress) val address =editStringText.text ) val locationAddress =GeoCodingLocation() locationAddress.getAddressFromLocation(ที่อยู่, บริบทแอปพลิเคชัน, GeoCoderHandler (นี้)) } } วัตถุสหาย { คลาสส่วนตัว GeoCoderHandler (วาลส่วนตัว mainActivity:MainActivity) :ตัวจัดการ () { แทนที่ fun handleMessage (ข้อความ:ข้อความ) { val locationAddress:String? locationAddress =เมื่อ (message.what) { 1 -> { val bundle =message.data bundle.getString("address") } else -> null } mainActivity.textViewLatLong.text =locationAddress } } }}

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

<ก่อนหน้า>นำเข้า android.content.Contextimport android.location.Addressimport android.location.Geocoderimport android.os.Bundleimport android.os.Handlerimport android.os.Messageimport android.util.Logimport java.io.IOExceptionimport java.util.*class GeoCodingLocation { val ส่วนตัว TAG ="GeoCodeLocation" สนุก getAddressFromLocation (locationAddress:String, บริบท:Context, handler:Handler ) { val thread =object :Thread () { แทนที่ fun run () { val geoCoder =Geocoder (บริบท Locale.getDefault () ) ผลลัพธ์ var:String? =null ลอง { val addressList =geoCoder.getFromLocationName(locationAddress, 1) if (addressList !=null &&addressList.size> 0) { val address =addressList.get(0) as Address val sb =StringBuilder() sb.append( address.latitude).append("\n") sb.append(address.longitude).append("\n") ผลลัพธ์ =sb.toString() } } catch (e:IOException) { Log.e (TAG, "ไม่สามารถเชื่อมต่อกับ GeoCoder", e) } ในที่สุด { val message =Message.obtain() message.target =handler message.what =1 val Bundle =Bundle () ผลลัพธ์ =("Address:$locationAddress" + "\n \nละติจูดและลองจิจูด:\n" + ผลลัพธ์) bundle.putString("ที่อยู่", ผลลัพธ์) message.data =บันเดิล message.sendToTarget() } } } thread.start () }}

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

     <หมวดหมู่ android:name="android.intent.category.LAUNCHER" />   

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

ฉันจะค้นหาละติจูดและลองจิจูดจากที่อยู่ใน Android โดยใช้ Kotlin ได้อย่างไร