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.
Related
This question already has answers here:
How to get complete address from latitude and longitude?
(23 answers)
Closed 1 year ago.
How to get complete address from latitude and longitude?
I want to get following values from Latitude and Longitude in android
Street Address
City / State
Zip
Complete Address
How to achieve this?
maybe using google map api can help you to get location detail
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0);
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();
For more info of available details, Look at Android-Location-Address
I have got a reference from and almost I have use this method - How to get complete address from latitude and longitude?
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
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(); // Only if available else return NULL
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
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
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'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..!