I am getting the AddressLine in local language(Hindi).
I was using Locale.getDefault(), thought that might be the reason.
But even after changing it to Locale.ENGLISH I get the same result.
List<Address> addresses = null;
try {
Geocoder gCoder = new Geocoder(c, Locale.ENGLISH);
addresses = gCoder.getFromLocation(lat, lng, 1);
Address addr = addresses.get(0);
String addressLine = addr.getAddressLine(0);
}catch{
}
I expected it to give the value in English, if that's not possible, return null so that i can go with something else other than AddressLine.
EDIT:
Tried this
Locale mLocale = new Locale("en");
Log.d("Display language = ", "" + mLocale.getDisplayLanguage());
Geocoder gCoder = new Geocoder(c, mLocale);
LogCat: Display language = English
But i get the same result.
Thank You
Related
I need to get the location name in arabic ,if gps enable for english i am using the following code.
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
// LogCat: Display language = English
Log.i("Display language = ", "" + mLocale.getDisplayLanguage());
Log.i("geocoder geocoder = ", "" + geocoder.toString());
// Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
if (null != listAddresses && listAddresses.size() > 0) {
String _Location = listAddresses.get(0).getAddressLine(0);
// Log.i("_Location = ", "" + _Location);
Address address = listAddresses.get(0);
Log.i("address = ", "" + address);
result = address.getLocality();
Log.i("result = ", "" + result);
// Toast.makeText(getApplicationContext(), "Your Location NAME is -" + result , Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
}
Address[addressLines=[
Abu Dhabi - United Arab Emirates"],feature=3rd
Street,admin=Abu Dhabi,sub-admin=null,locality=Abu
Dhabi,thoroughfare=3rd
Street,postalCode=null,countryCode=AE,countryName=United Arab
Emirates,hasLatitude=true,latitude=,hasLongitude=true,longitude=,phone=null,url=null,extras=null]
i need the locality value in arabic based on user selected language in app.
i given Locale mLocale = new Locale("ar","AE"); but it is giving country name comming arabic i need locality value in arabic.
in galaxy s7 data coming correctly based on arabic selection giving arabic data
s6 not comming it is giving english data only if i pass locale arabic also.
In recent reference, Locale has information of language/country code which are suitable in ISO 639-1/ISO 3166-1.
ISO 639-1 is two-letter lowercase language code. Arabic is defined ar in this format.
You can set locale for Arabic like this :
Locale loc = new Locale("ar");
Note:
Android Locale reference: https://developer.android.com/reference/java/util/Locale.html
ISO 639-1 code reference: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
hope that its works for you.
I want to do an application which gets the current address; country name, city name and district name. And then I have 3 spinner which are having all country names, city names and district names in Turkish.
When I get the address using GPS and Geocoder, I want to match them with the names in my spinners.
The problem is the address coming from geocoder is in English. And so, it does not match with my countries in my spinner. So I cannot select true country with programatically.
Here is my related code:
//In here I add countries coming from web service into countries array.
for(int i=0;i<responseInfo.size();i++) {
String name = responseInfo.get(i).getName();
countries.add(name);
//If country name is equals to country name coming from gps
//then I hold it in selectedCountry variable.
//But "Turkey" != "Türkiye" so this if block does not work
if(name.equalsIgnoreCase(foundAddress.getCountryName()))
{
selectedCountryId = (String)responseInfo.get(i).getId();
selectedCountry = responseInfo.get(i);
}
}
//In this method, when I get country name from the item in addresses array
//it returns in English
public List<Address> findAddressFromLatLng(double latitude, double longitude)
{
Geocoder geocoder;
List<Address> addresses = null;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if ((addresses != null) && (addresses.size() != 0)) {
return addresses;
}
return null;
}
In another device, I have no problem. Country name coming from geocoder is in Turkish (which means Geocoder have Turkish names also). Probably it is because of device language.
I tried to change this line but I can't find Turkish as locale:
geocoder = new Geocoder(this, Locale.getDefault());
Thanks in advance.
Docs for Geocoder suggest that setting a Locale is exactly what you need. There's no predefined Locale for Turkish, but you can easily define one:
Locale locale = new Locale("tr", "TR");
i am trying to show the address using gps location.
error: context cannot be resolved to a variable
Geocoder geocoder = new Geocoder(context,Locale.getDefault());
Address address;
String result = null;
List<Address> list = geocoder.getFromLocation(latitude, longitude, 1);
address = list.get(0);
result = address.getAddressLine(0) + ", " + address.getLocality();
you didn't have initialized context that's why this error is throwing.
This is my code
It don't have any error but
It shows in textView "No Address returned!"
double lat = 18.520430300000000000, log = 73.856743699999920000;
String address = null;
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
StringBuilder sb = null;
try {
List<Address> addresses = geocoder.getFromLocation(lat, log, 1);
sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)+ ",");
txtAdd.setText("Address :"+sb);
}
else{
txtAdd.setText("No Address returned!");
}
} catch (Exception e) {
// TODO Auto-generated catch block
/*e.printStackTrace();*/
txtAdd.setText("Canont get Address!");
}
Plz give me solution on this
Thanks in Advance...
The doku gives one hint, maybe this should help
http://developer.android.com/reference/android/location/Geocoder.html
Returns an array of Addresses that are known to describe the area
immediately surrounding the given latitude and longitude. The returned
addresses will be localized for the locale provided to this class's
constructor.
The returned values may be obtained by means of a network lookup. The
results are a best guess and are not guaranteed to be meaningful or
correct. It may be useful to call this method from a thread separate
from your primary UI thread.
I'm trying to get the street name of my current location but I can't seem to get it.
I use this method to retrieve the Address:
public Address getAddressForLocation(Context context, Location location) throws IOException {
if (location == null) {
return null;
}
double latitude = location.getLatitude();
double longitude = location.getLongitude();
int maxResults = 1;
Geocoder gc = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);
if (addresses.size() == 1) {
return addresses.get(0);
} else {
return null;
}
}
And then I can do things like. address.getLocality() and address.getPostalCode()
But what I want is the street name. Like in "Potterstreet 12". When I print the AddressLine(0) and AddressLine(1) I only get the postalcode, city and country.
How can I retrieve the street name of the position i'm currently at?
Have you tried using getAddressLine ?
See here for more info on this method
Something like this should do (untested):
for (int i = 0; i < addresses.getMaxAddressLineIndex(); i++) {
Log.d("=Adress=",addresses.getAddressLine(i));
}
Try something like this in your code
String cityName=null;
Geocoder gcd = new Geocoder(getBaseContext(),Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(location.getLatitude(), location
.getLongitude(), 1);
if (addresses.size() > 0)
StreetName=addresses.get(0).getThoroughfare();
String s = longitude+"\n"+latitude +
"\n\nMy Currrent Street is: "+StreetName;
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
it works for me :-) Good luck ;-)
If you have a complete address (city + street), in
address.getAddressLine(0)
you find the street name and number.
getFromLocation wasn't working for me either. There are a couple steps you can take.
1. First off go into gradle and make sure you are using the latest play services lib.
2. Don't over specify, the reason I got no results is because I had to much info in my address. When I removed the postal code I got results every time.
3. Try the online api:
http://maps.google.com/maps/api/geocode/json?address=192%20McEwan%20Dr%20E,%20Caledon,%20ON&sensor=false
Just replace the address in there with yours.
Good luck
I had a very similar problem but with the Country name, this is the function I ended up using:
function getCountry(results) {
var geocoderAddressComponent,addressComponentTypes,address;
for (var i in results) {
geocoderAddressComponent = results[i].address_components;
for (var j in geocoderAddressComponent) {
address = geocoderAddressComponent[j];
addressComponentTypes = geocoderAddressComponent[j].types;
for (var k in addressComponentTypes) {
if (addressComponentTypes[k] == 'country') {
return address.long_name;
}
}
}
}
return 'Unknown';
}
You should be able to adapt this to get the street name out without much fuss.
Inspired by this answer
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses =
gcd.getFromLocation(currentLatitude, currentLongitude,100);
if (addresses.size() > 0 && addresses != null) {
StringBuilder result = new StringBuilder();
myaddress.setText(addresses.get(0).getFeatureName()+"-"+addresses.get(0).getLocality()+"-"+addresses.get(0).getAdminArea()+"-"+addresses.get(0).getCountryName());
}
getfeaturename() return Streetname
getlocality() return city
getadminarea() return State
That's All..!