SCENARIO:
I am creating an app which fetches user current location, and using the fetched latitude and longitude, I find address from it using the below code:
private String getAddress(double latitude,double longitude) {
Geocoder geocoder;
List<Address> addresses;
String adress="";
geocoder=new Geocoder(getApplicationContext(),Locale.ENGLISH);
try {
addresses=geocoder.getFromLocation(latitude,longitude,1);
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();
adress=address+","+city+","+state+","+country+","+postalCode;
// Here 1 represent max location result to returned, by documents it recommended 1 to 5
} catch (IOException e) {
e.printStackTrace();
}
return adress;
}
And address alongwith latitude and longitude is sent to server using web services.
PROBLEM:
This code works fine in my country(i.e India) but in another country like Kazakhstan, I don't get address in proper English Language. It looks something like this.
When I searched for solving this problem I came around following solutions:
- Earlier I was using Default Locale for getting address from lat, long which was causing issue. So I have changed Locale to English which somewhat solved the problem as it now returns in English.
- I also changed database collation in php backend to UTF-8 General CI but problem still exist.
I tested my app by using mock location apps but was not able to reproduce this issue.
What are the possible cause and solution for this?
Finally I was able to resolve this problem. It was a php backend issue. To solve this I had to add following line after connecting to database.
For furhter reference check this link
Alongwith this there was problem with Multipart Request too. I was sending location address alongwith other parameter via Multipart request using standard http library, which was causing issue when entered in php database.
To solve this we use
UrlEncode/Decode
on app as well as backend side. So we sent address in encoded form and when decoded on server side, address was proper this time.Hope this can help anyone.