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
Related
I'm trying to get users city by location, but I can't get ONLY the City!
I tried pretty much everything I believe but nothing seem to work, or, actually, the only thing working for getting user-info by location is "getAddressLine(0)".
Below is code
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(context, Locale.getDefault());
String city="unknown";
try {
addresses = geocoder.getFromLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude(), 1);
} catch (Exception e) {
Log.d(TAG, "getAddress failed: " + e.getMessage());
e.printStackTrace();
return "Couldn't find city!";
}
if(addresses != null && addresses.size() > 0) {
city = addresses.get(0).getAddressLine(0); // Returns full address!
// city = addresses.get(0).getLocality(); // Returns only an empty string " ".
}
return city;
So, I wonder how I could get only CITY and not full address. Also, WHY is "getLocality()" returning an useless empty string?
I'm still wonder why is getLocality() don't work,
but I created a temporarily solution as below code:
if(addresses != null && addresses.size() > 0) {
String address = addresses.get(0).getAddressLine(0); // StreetAddress -> StreetAddress Number -> Postal Code -> City -> Country
String[] splitAddress = address.split(" "); // Splits address where there is a space, creating array with 5 index. Index 4 should be CITY!
city=splitAddress[4].substring(0,splitAddress[4].length()-1); // Saves city into city-string, and removes a comma from the getAddressLine(0)!
Log.d("SplittedAddress", Arrays.toString(splitAddress));
}
If you want the city, try:
String city = addresses.get(0).getAddressLine(1);
Adress.get(1).getAddressLine(0);
i am trying to show the address using gps location.
error: context cannot be resolved to a variable
Geocoder geocoder = new Geocoder(context,Locale.getDefault());
Address address;
String result = null;
List<Address> list = geocoder.getFromLocation(latitude, longitude, 1);
address = list.get(0);
result = address.getAddressLine(0) + ", " + address.getLocality();
you didn't have initialized context that's why this error is throwing.
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
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.
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.