Untuk menampilkan lokasi alamat user berada, kita dapat menggunakan fasilitas dari Google. Misalnya kita memiliki file Main.java, maka pastikan Activity mengimplementasikan LocationListener. Kemudian masukkan kode berikut pada public void onLocationChanged
@Override
public void onLocationChanged(Location lokasi) {
// TODO Auto-generated method stub
try {
in_lati = lokasi.getLatitude();
in_longi = lokasi.getLongitude();
} catch (Exception e) {
Log.e("Info", "data tidak bisa diambil");
}
}
Untuk mengambil data lokasi alamat dari Google taruhlah dalam public void onCreate(Bundle savedInstanceState)
geocoder = new Geocoder(this, Locale.ENGLISH);
double a = in_lati, b = in_longi;
try {
List
addresses = geocoder.getFromLocation(a, b, maxResult);
if (addresses != null) {
for (int j = 0; j < maxResult; j++) {
Address returnedAddress = addresses.get(j);
StringBuilder strReturnedAddress = new StringBuilder();
for (int i = 0; i < returnedAddress
.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(
returnedAddress.getAddressLine(i)).append(", ");
}
addressList[j] = strReturnedAddress.toString();
}
lokasimu.setText("Lokasi Anda: " + addressList[1]);
} else {
lokasimu.setText("Lokasi belum ditemukan");
}
} catch (Exception e) {
// TODO: handle exception
lokasimu.setText("Lokasi returned!");
}

إرسال تعليق