Getting street name from Address/Location object in Android - android

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..!

Related

using Geocoder to return a list of all street names of a selected city in android

if i have a city retrieved by:
Geocoder gcd = new Geocoder(context, Locale.getDefault());
Address address = gcd.getFromLocation(lat, lng, 1);
Is there any way to get list of all street names in a selected town using the google maps api?
UPDATE
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (addresses.size() > 0)
for (int i = 0; i < addresses.get(0).getMaxAddressLineIndex(); i++)
Log.e("afaf", " " + addresses.get(0).getAddressLine(i));
this returns (for example): new york, 5th street 3924.And it doesn't iterate on all city streets.
I think what your looking for is this:
getFromLocationName (String locationName, int maxResults)
From the docs :
Returns an array of Addresses that are known to describe the named
location, which may be a place name such as "Dalvik, Iceland", an
address such as "1600 Amphitheatre Parkway, Mountain View, CA", an
airport code such as "SFO", etc.. The returned addresses will be
localized for the locale provided to this class's constructor.
The query will block and returned values will 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.
to get the Street name use this(I think you already know this):
for(int a = 0 ; a < addresses.size() ; a++){
Address address = addresses.get(a);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
// get address like address.getAddressLine(i);
}
}
hope it helps :)

how to get complete place address using latitude and longitude on emulator in android?

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.

Get Local Cities names using Google Map API

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;
}

Android reverse geocoding getLocality returns often null

I am using Android Geocoding to get the current city with Address.getLocality(). It has worked fine, until just recently it appears to often return null for the locality.
Here is an example:
try {
Geocoder c = new Geocoder(this, Locale.getDefault());
double lat = 51.481;
double lon = 0.0;
List<Address> l = c.getFromLocation(lat, lon, 5);
for (Address a: l) {
Log.i("GeocoderTest", "Locality " + a.getLocality() + " (" + a + ")");
}
} catch (IOException e) {
Log.e("GeocoderTest", "", e);
}
This now logs the following message for the first returned address:
Locality null (Address[addressLines=[0:"14-18 Park Vista",1:"London
Borough of Greenwich, London
SE10",2:"UK"],feature=,admin=null,sub-admin=null,locality=null,thoroughfare=Park
Vista,postalCode=null,countryCode=GB,countryName=United
Kingdom,hasLatitude=true,latitude=51.4819069,hasLongitude=true,longitude=-6.327E-4,phone=null,url=null,extras=null])
Some locations do return the city in the locality, while a location next to it does not.
So it did work just fine before, actually I had not seen a null locality before. So I guess something must have changed in Google's geocoding service. Any idea what is going on, and is this situation permanent? If yes, what would be the best way to determine the city from a location?
I noticed, that very often getLocality() returns null for the first address in the list, returned by Geocoder.
On the other hand correct city name stays at Locality of a next Address.
So I am using this workaround and it works well for big cities:
private String getCityNameByCoordinates(double lat, double lon) throws IOException {
List<Address> addresses = mGeocoder.getFromLocation(lat, lon, 10);
if (addresses != null && addresses.size() > 0) {
for (Address adr : addresses) {
if (adr.getLocality() != null && adr.getLocality().length() > 0) {
return adr.getLocality();
}
}
}
return null;
}
Now I live in Canada, Ontario, Hamilton (Hamilton is my city, Ontario is the province)
I noticed that getLocality() returns null, and getAdminArea() returns Ontario, and getSubLocality() returns Hamilton. ch
In Kotlin I have done something like this, given the address a
var place = a.locality
if (place == null) place = a.subLocality
if (place == null) place = a.subAdminArea
if (place == null) place = a.adminArea
It works even for remote places
Using getSubAdminArea() worked fine for me.

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