I am testing an app which takes latitude and longitude with help of GPS in Samsung Tablet. I am able to capture current latitude, longitude from GPS unit. Now I want to get address from these location using latitude longitude without use of Internet i.e. wi-fi, mobile data.
I am doing it below manner:
public String getAddress(Context ctx, double latitude, double longitude) {
StringBuilder result = new StringBuilder();
try {
Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
String locality=address.getLocality();
String city=address.getCountryName();
String region_code=address.getCountryCode();
String zipcode=address.getPostalCode();
double lat =address.getLatitude();
double lon= address.getLongitude();
result.append(locality+" ");
result.append(city+" "+ region_code+" ");
result.append(zipcode);
}
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
return result.toString();
}
private class GetCurrentAddress extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// this lat and log we can get from current location but here we given hard coded
double latitude=12.916523125961666;
double longitude=77.61959824603072;
String address= getAddress(context, latitude, longitude);
return address;
}
@Override
protected void onPostExecute(String resultString) {
dialog.dismiss();
result.setText(resultString);
}
}
In mainfest i do below entries:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
It is not possible to get Address from Lat Lang without internet because google also do geocoding
Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
this getFromLocation() method should be run in asynTask and with internet then it gives desired result.
<uses-permission android:name="android.permission.INTERNET" />
Internet permission also need