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 getAddre
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());
}
That's All..!
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
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 ;-)
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));
}