Get User Current Location name in Arabic? - android

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.

Related

Address coming from Geocoder is in English

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");

Why am i getting location details in local language?

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

geocoder.getFromLocation returning null

my code :
try
{
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(LocationActivity.this, Locale.getDefault());
addresses = geocoder.getFromLocation(longitude,latitude,1);
Toast.makeText(LocationActivity.this, addresses.size() + "", Toast.LENGTH_LONG).show();
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(0);
String country = addresses.get(0).getAddressLine(0);
Toast.makeText(LocationActivity.this, address + " " + city + " " + country, Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
Toast.makeText(LocationActivity.this,e.toString(), Toast.LENGTH_LONG).show();
}
this is my code to get address from longitude and latitude.
but every time addresses.size returns 0
can anyone help me for this
I find the solution...
hold Ctrl on getFromLocation() and go to Geocoder.class .
then download sources for your android API and restart your project.
then you see its work
if you are using emulator go to DDMS you will find emulator control and give your valid lat lng in that and press send..
and Batter make sure that mention Internet permissions in manifest...
try {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
double longitude = 23.00000;
double latitude = 72.00000;
addresses = geocoder.getFromLocation(longitude, latitude, 1);
Toast.makeText(this, addresses.size() + "", Toast.LENGTH_LONG)
.show();
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
if(address==null)
address="";
if(city==null)
city="";
if(country==null)
country="";
Toast.makeText(this, address + " " + city + " " + country,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
Im not sure if this will help, but I think you are getting an IOException. Look at the LogCat. Also see this
The Geocoder query methods will return an empty list if there no backend service in the platform.
ref - http://developer.android.com/reference/android/location/Geocoder.html
Fixed it with a condtion address.size() > 0 in the try loop.
I didn't really found out where the real problem is , but it seems that the loop is happening twice, and once too early , because i could have the loop happened twice , one empty result and one good result.
Maybe coming from me as as two map and date passing through intent.
But fixed for me

Android how to get the street name from an address returned by Geocoder

I'm using Geocoder in reverse way to get an address from a given lat & lon.
Do you know how to get from Address only the street name?
Geocoder geocoder = new Geocoder(AutoFinderMapActivity.this);
try {
List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1);
if (addressList != null && addressList.size() > 0) {
// Help here to get only the street name
String adress = addressList.get(0).get...;
}
} catch (IOException e) {
e.printStackTrace();
}
Thanks in advance,
I was searching up the very same thing. I disagree with the answer that was marked as correct. I could be wrong but it would appear that "Thoroughfare" (what a quaint old English term!) is the field that provides the street name. so for example:
get the addresses:
List<Address> addresses = null;
addresses = geocoder.getFromLocation(latitude, longitude,1);
if(addresses != null && addresses.size() > 0 ){
Address address = addresses.get(0);
// Thoroughfare seems to be the street name without numbers
String street = address.getThoroughfare();
}
And the function you might get the street number from is getSubThoroughfare().
This is an example of my code, and show addrees, city, etc.. I hope this help you..
try {
List<Address> addresses;
Geocoder geocoder= new Geocoder(MyactivityName.this);
addresses = geocoder.getFromLocation(Marker.getPosition().latitude,Marker.getPosition().longitude,1);
if(addresses != null && addresses.size() > 0 ){
Address address = addresses.get(0);
String province = addresses.get(0).getAdminArea();
Marker.setSnippet(address.getThoroughfare()+", "+province);
}
} catch (IOException e) {
e.printStackTrace();
}
Marker.showInfoWindow();
//get current Street name
String address = addresses.get(0).getAddressLine(0);
//get current province/City
String province = addresses.get(0).getAdminArea();
//get country
String country = addresses.get(0).getCountryName();
//get postal code
String postalCode = addresses.get(0).getPostalCode();
//get place Name
String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL
System.out.println("Street: " + address + "\n" + "City/Province: " + province + "\nCountry: " + country
+ "\nPostal CODE: " + postalCode + "\n" + "Place Name: " + knownName);
if you look more information or an example look this Link
Take a look at the Address class.
Android Developer: Address

ZipCode from location

Is there any way to get zip-code of the current user location from location or lat or long.
If so how?
Use Android Geocoder API!
getFromLocation(double, double, int) is all you need.
Yes you can...All you need to do is extract the
http://maps.google.com/maps/geo?ll=latitude,longitude
http://maps.google.com/maps/geo?ll=10.345561,123.896932
Or try experimenting this solutions.I can give you an idea.
You can have a city right? WIth this json data, If you have a database(at maxmind.com) of the zipcode with this format
CountryCode | City | ZipCode
PH |Cebu |6000
Now query your database that is relative to the countrycode that googles return.
UPDATE
The Geocoding API v2 has been turned down on September 9th, 2013.
Here's the new URL of API service
http://maps.googleapis.com/maps/api/geocode/json?latlng=your_latitude,your_longitude
http://maps.googleapis.com/maps/api/geocode/json?latlng=10.32,123.90&sensor=true
Geocoder based implementation:
private String getZipCodeFromLocation(Location location) {
Address addr = getAddressFromLocation(location);
return addr.getPostalCode() == null ? "" : addr.getPostalCode();
}
private Address getAddressFromLocation(Location location) {
Geocoder geocoder = new Geocoder(this);
Address address = new Address(Locale.getDefault());
try {
List<Address> addr = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addr.size() > 0) {
address = addr.get(0);
}
} catch (IOException e) {
e.printStackTrace();
}
return address;
}
You can get also other information from address, for example city name.

Categories

Resources