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");
Related
whenever I click on a particular location in Google Map, I want that location's name to be displayed in a text view. Is there any way I can do it?
Get lat_lng value for particular location, then lat lng value pass to geocoder method.
public void getgetLocationAddress(Context context,double lat,double lng){
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(context, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(lat, lng, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
country = addresses.get(0).getCountryName();
// System.out.println("SDK_DATA"+address+"..."+city +country);
//Here address set to your textview
} catch (IOException e) {
e.printStackTrace();
} }
In my android app, I have to list all local cities names using my current location with Google Map API
Input
Current Co-ordinates
Current place Name.
Output
Local cities of current place.
You must collect the latitude and longitude first.
then you can user geocoder to get it. Like :-
Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0)
System.out.println(addresses.get(0).getLocality());
Try this way
public List<Address> getAddressListFromLatLong(double lat, double lng) {
Geocoder geocoder = new Geocoder(this);
List<Address> list = null;
try {
list = geocoder.getFromLocation(lat, lng, 20);
// 20 is no of address you want to fetch near by the given lat-long
for (Address address : list) {
System.out.println(address);
}
} catch (Throwable e) {
e.printStackTrace();
}
return list;
}
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
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.
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..!