When I try to get default locale it returns "zz_ZZ":
countryCode: "ZZ"
languageCode: "zz"
Code is:
private String getAddressFromLocation(Location location) {
Geocoder geoCoder = new Geocoder(this.getApplicationContext(), Locale.getDefault());
List<Address> matches = null;
try {
matches = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
Address bestMatch = (matches.isEmpty() ? null : matches.get(0) );
if (bestMatch != null){
String fullAddress = bestMatch.getAddressLine(0);
for (int i = 1; i <= bestMatch.getMaxAddressLineIndex(); i++){
fullAddress += ", " + bestMatch.getAddressLine(i);
}
return fullAddress;
}else{
return Constants.EMPTY_STRING;
}
} catch (IOException e) {
ACRA.getErrorReporter().handleSilentException(new MyGeoLocationAddressException(TAG + " ERROR CREATING ADDRESS - " + e.getMessage()));
return Constants.EMPTY_STRING;
}
}
Do I need to setDefaultLocale? What does ZZ_zz mean?
Second problem is:
If I use the "ZZ_zz" default locale, I don't get any address but if I use:
Locale.setDefault(Locale.ENGLISH);
I get the exact address.
ZZ_zz is the identifier for the Accented English locale and is mostly used for finding missing translations in your app.
I don't think there's a best practice on how to handle accented english, but treating it as english would probably be the best option.
You must handle locations in this way, check the scenario when no address is returned
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
location.getLatitude(),
location.getLongitude(),
// In this sample, get just a single address.
1);
} catch (IOException ioException) {
// Catch network or other I/O problems.
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " +
"Latitude = " + location.getLatitude() +
", Longitude = " +
location.getLongitude(), illegalArgumentException);
}
// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();
// Fetch the address lines using getAddressLine,
// join them, and send them to the thread.
for(int i = 0; i < address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
Log.i(TAG, getString(R.string.address_found));
deliverResultToReceiver(Constants.SUCCESS_RESULT,
TextUtils.join(System.getProperty("line.separator"),
addressFragments));
}
}
Related
try {
addresses = gcd.getFromLocation(place.getLatLng().latitude, place.getLatLng().longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses.size() > 0) {
String toastMsg = String.format("Place: %s", addresses.get(0).getLocale() + " - " + addresses.get(0).getCountryName() + " - " + addresses.get(0).getCountryCode());
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
}
I found solution thank you all developers android
Geocoder gcd = new Geocoder(getActivity(), Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation(place.getLatLng().latitude, place.getLatLng().longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses.size() > 0) {
/*
getCity --> addresses.get(0).getLocality()
getCountry --> addresses.get(0).getCountryName()
getCodeCountry --> addresses.get(0).getCountryCode() (MA)
*/
city = String.format("%s",addresses.get(0).getLocality());
country= String.format("%s",addresses.get(0).getCountryName());
tvCountry.setText(country);
tvCity.setText(city);
}
I am trying to get location name or address. I have successfully fetched the latitude and longitude by Google Fused Location API.
Now I want to fetch the location address (example : City name,Road no, or specific address) by using the latitude and longitude.
For this purpose I am using Google Geocoder and it works fine. But in some devices the location address returns the null value.
I searched in the net for the solution of this problem and found that some device manufacturers does not include this feature in their devices. That's why those device can't find the the address by Reverse Geocoding method. Here is the link of that information
So is there any alternative way to find the address name as string without Geoconding ?
Here is my code for fetching address by Geocoding
public static void getAddress(Context context, double LATITUDE, double LONGITUDE) {
try {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null && addresses.size() > 0) {
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL
Log.d(TAG, "getAddress: address" + address);
Log.d(TAG, "getAddress: city" + city);
Log.d(TAG, "getAddress: state" + state);
Log.d(TAG, "getAddress: postalCode" + postalCode);
Log.d(TAG, "getAddress: knownName" + knownName);
}
} catch (IOException e) {
e.printStackTrace();
}
return;
}
Use this its working for me
public class LocationAddress {
private static final String TAG = "LocationAddress";
private static String area = null;
public static void getAddressFromLocation(final double latitude, final double longitude,
final Context context, final Handler handler) {
Thread thread = new Thread() {
#Override
public void run() {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = null;
try {
List<Address> addressList = geocoder.getFromLocation(
latitude, longitude, 1);
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append("\n");
}
area = address.getSubLocality();
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
result = sb.toString();
}
} catch (IOException e) {
Log.e(TAG, "Unable connect to Geocoder", e);
} finally {
Message message = Message.obtain();
message.setTarget(handler);
if (result != null) {
message.what = 1;
Bundle bundle = new Bundle();
/*result = "Latitude: " + latitude + " Longitude: " + longitude +
"\n\nAddress:\n" + result;*/
bundle.putString("address", result);
bundle.putString("area",area);
message.setData(bundle);
} else {
message.what = 1;
Bundle bundle = new Bundle();
/* result = "Latitude: " + latitude + " Longitude: " + longitude +
"\n Unable to get address for this lat-long.";*/
bundle.putString("address", result);
bundle.putString("area",area);
message.setData(bundle);
}
message.sendToTarget();
}
}
};
thread.start();
}
}
To call this in your Activity use
LocationAddress locationAddress = new LocationAddress();
locationAddress.getAddressFromLocation(latitude,longitude,
getApplicationContext(), new GeocoderHandler());
GeocoderHandler
private class GeocoderHandler extends Handler {
#Override
public void handleMessage(Message message) {
String locationAddress = null;
String area = null;
switch (message.what) {
case 1:
Bundle bundle = message.getData();
locationAddress = bundle.getString("address");
area = bundle.getString("area");
break;
default:
locationAddress = null;
}
if(locationAddress != null)
locationAddress=locationAddress.replaceAll("[\r\n]+", " ");
Log.d("===========>>>",area);
Log.d("===========>>>>",locationAddress);
}
}
I am working with Geocoder to find exact address of a location using latitude and longitude, but it didn't support in lollipop and above
Geocoder geo = new Geocoder(getApplicationContext(), Locale.getDefault());
String mylocation;
if (Geocoder.isPresent()) {
try {
List < Address > addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
String addressText = String.format("%s, %s, %s",
// If there's a street address, add it
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
// Locality is usually a city
address.getLocality(),
// The country of the address
address.getCountryName());
mylocation = "Lattitude: " + location.getLatitude() + " Longitude: " + location.getLongitude() + "\nAddress: " + addressText;
address1.setText(addressText);
}
} catch (IOException e) {
e.printStackTrace();
}
}
You can use following code for Address:
//Set Address
try {
List < Address > addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses != null && addresses.size() > 0) {
String address = addresses.get(0).getAddressLine(0) + " " + addresses.get(0).getAddressLine(1);
if (address != null) {
textViewAddress.setText(address);
} else {
textViewAddress.setText("Not Available");
}
String city = addresses.get(0).getAddressLine(2);
if (city != null) {
textViewCity.setText(city);
} else {
textViewCity.setText("Not Available");
}
String state = addresses.get(0).getAdminArea();
if (state != null) {
textViewState.setText(state);
} else {
textViewState.setText("Not Available");
}
String country = addresses.get(0).getCountryName();
if (country != null) {
textViewCountry.setText(country);
} else {
textViewCountry.setText("Not Available");
}
System.out.println("Address >> " + address + " " + " \n" + state + " \n" + country);
}
} catch (IOException e) {
e.printStackTrace();
}
Or
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL
refer following link: Get Address
In my app i have used google map, based on the latitude and longitude successfully i can get the Area,Sate,pincode, country, my code is below
Geocoder gcd = new Geocoder(Map.this, Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(draggedlat, draggedlng,
1);
if (addresses.size() > 0) {
Log.d("CityName", "---->"
+ addresses.get(0).getLocality()
+ addresses.get(0).getAddressLine(1)
+ addresses.get(0).getAddressLine(2)
+ addresses.get(0).getFeatureName());
Log.d("Locality", "Locality"
+ addresses.get(0).getSubLocality());
} else {
Toast.makeText(Map.this,
"Try with some other city..",
Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Now my problem is have to find the city name of a particular area. is that possible?? kindly help me.
My OutPut Is:
VanapatlaVanapatla, Andhra Pradesh 509235,India,Kollapur Rd
Thanks in advance!
Try this with different points:
List<Address> list = geoCoder.getFromLocation(location
.getLatitude(), location.getLongitude(), 1);
if (list != null & list.size() > 0) {
Address address = list.get(0);
result = address.getLocality();
return result;
I reffred many questions from Stack overflow and implemented the above procedure. But I am unable to get the adress. Please let me know If i missed something.. ?
myLoc = (TextView) findViewById(R.id.id1);
Geocoder geocoder = new Geocoder(getBaseContext(),Locale.getDefault());
try {
address = geocoder.getFromLocation(latitude, longitude, 1);
if (address.size() > 0) {
for (int i = 0; i < address.get(0)
.getMaxAddressLineIndex(); i++) {
display = "";
display += address.get(0).getAddressLine(i)
+ "\n";
}
}
} catch (Exception e2) {
// TODO: handle exception
}
myLoc.setText("Current Location:"+display);
System.out.println(display);
You can use Reverse geo coding to and Google apis to get address from latitude and longitude.
Reverse Geo Coding:
double currentLatitude;
double currentLongitude;
void getAddress(){
try{
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses =
gcd.getFromLocation(currentLatitude, currentLongitude,100);
if (addresses.size() > 0) {
StringBuilder result = new StringBuilder();
for(int i = 0; i < addresses.size(); i++){
Address address = addresses.get(i);
int maxIndex = address.getMaxAddressLineIndex();
for (int x = 0; x <= maxIndex; x++ ){
result.append(address.getAddressLine(x));
result.append(",");
}
result.append(address.getLocality());
result.append(",");
result.append(address.getPostalCode());
result.append("\n\n");
}
addressText.setText(result.toString());
}
}
catch(IOException ex){
addressText.setText(ex.getMessage().toString());
}
}
Google API: See this api which retrun address from latitude and longitude
http://maps.googleapis.com/maps/api/geocode/json?latlng=17.734884,83.299507&sensor=true
To know more read this
getMaxAddressLineIndex() returns an index which start from zero and thus your for-loop condition should be 0 <= maxIndex instead of 0 < maxIndex
You overwrite previous address lines on every iteration by assigning display = ""; and thus will end up with the last address line only. Is that on purpose?
Another good idea is to implement the LocationListener interface and register your Activity as a listener using LocationManager requestLocationUpdates() method. You can then override onLocationUpdate() to be informed whenever the location of the device changes. You provide the requestLocationUpdates() method the minimum amount of time that must pass before you will accept another update and how far the device must move before you get an update.
You can do like this to get complete address. In case you want country name
, state etc seperately .Then, I will not prefer you this method .
public class ParentHomeActivity extends AppCompatActivity {
...
private Geocoder geocoder;
private TextView mAddressTxtVu;
...
// I assume that you got latitude and longitude correctly
mLatitude = 20.23232
mLongitude = 32.999
String errorMessage = "";
geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
mlattitude,
mlongitude,
1);
} catch (IOException e) {
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, e);
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " + "Latitude = " + mlattitude +",
Longitude = " + mlongitude, illegalArgumentException);
}
// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();
// Fetch the address lines using getAddressLine,
// join them, and send them to the thread.
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
// Log.i(TAG, getString(R.string.address_found));
mAddressTxtVu.setText(TextUtils.join(System.getProperty("line.separator"),
addressFragments));
}