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

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


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

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

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

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

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

<ก่อนหน้า>นำเข้า android.support.v7.app.AppCompatActivity;นำเข้า android.os.Bundle;นำเข้า android.os.Handler;นำเข้า android.os.ข้อความ;นำเข้า android.view.View;นำเข้า android.widget.Button;นำเข้า android.widget.EditText; นำเข้า android.widget.TextView; MainActivity คลาสสาธารณะขยาย AppCompatActivity { ปุ่มที่อยู่ปุ่ม; TextView textViewAddress; TextView textViewLatLong; @Override ป้องกันโมฆะ onCreate (Bundle saveInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); textViewAddress =findViewById (R.id.textViewAddress); textViewLatLong =findViewById(R.id.latLongTV); addressButton =findViewById(R.id.addressButton); addressButton.setOnClickListener (View.OnClickListener ใหม่ () { @แทนที่โมฆะสาธารณะ onClick (ดู arg0) { EditText editText =findViewById (R.id.editTextAddress); ที่อยู่สตริง =editText.getText ().toString (); GeoCodingLocation locationAddress =ใหม่ GeoCodingLocation(); locationAddress.getAddressFromLocation(ที่อยู่, getApplicationContext(), ใหม่ GeoCoderHandler()); } }); } GeoCoderHandler คลาสส่วนตัวขยาย Handler { @Override public void handleMessage (ข้อความข้อความ) { String locationAddress; สวิตช์ (message.what) { กรณีที่ 1:Bundle Bundle =message.getData (); locationAddress =bundle.getString("ที่อยู่"); หยุดพัก; ค่าเริ่มต้น:locationAddress =null; } textViewLatLong.setText (ที่อยู่ที่อยู่); } }}

ขั้นตอนที่ 4 − สร้างคลาส java (GeoCodeLocation.java) และเพิ่มรหัสต่อไปนี้ -

<ก่อนหน้า>นำเข้า android.content.Context;นำเข้า android.location.Address;นำเข้า android.location.Geocoder;นำเข้า android.os.Bundle;นำเข้า android.os.Handler;นำเข้า android.os.Message;นำเข้า android.util บันทึก; นำเข้า java.io.IOException; นำเข้า java.util.List; นำเข้า java.util.Locale; คลาส GeoCodeLocation { ส่วนตัว สแตติกสตริงสุดท้ายสุดท้าย ="GeoCodeLocation"; โมฆะคงที่สาธารณะ getAddressFromLocation (ตำแหน่งสตริงสุดท้ายที่อยู่บริบทบริบทสุดท้ายตัวจัดการตัวจัดการสุดท้าย) { เธรดเธรด =เธรดใหม่ () { @Override การเรียกใช้โมฆะสาธารณะ () { Geocoder geocoder =ใหม่ Geocoder (บริบท Locale.getDefault ()); ผลลัพธ์สตริง =null; ลอง { รายการ addressList =geocoder.getFromLocationName (locationAddress, 1); if (addressList !=null &&addressList.size()> 0) { ที่อยู่ที่อยู่ =(ที่อยู่) addressList.get(0); StringBuilder sb =ใหม่ StringBuilder (); sb.append(address.getLatitude()).append("\n"); sb.append(address.getLongitude()).append("\n"); ผลลัพธ์ =sb.toString(); } } catch (IOException e) { Log.e (TAG, "ไม่สามารถเชื่อมต่อกับ Geocoder", e); } ในที่สุด { ข้อความ =Message.obtain(); message.setTarget (ตัวจัดการ); ถ้า (ผลลัพธ์ !=null) { message.what =1; บันเดิลบันเดิล =บันเดิลใหม่ (); ผล ="ที่อยู่:" + locationAddress + "\n\nละติจูดและลองจิจูด :\n" + ผล; bundle.putString("ที่อยู่", ผลลัพธ์); message.setData(มัด); } อื่น ๆ { message.what =1; บันเดิลบันเดิล =บันเดิลใหม่ (); ผลลัพธ์ ="ที่อยู่:" + locationAddress + "\n ไม่สามารถรับละติจูดและลองจิจูดสำหรับตำแหน่งที่อยู่นี้"; bundle.putString("ที่อยู่", ผลลัพธ์); message.setData(มัด); } message.sendToTarget(); } } }; thread.start(); }}

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

    <กิจกรรม android:name=".MainActivity">      

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

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

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