How to get complete address from latitude and longitude in android? [duplicate] - android

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

Related

android-How to check location is in special area?

I want to use google map in my android app in specific area. for example in special country. How can I check that my current location is in that place?
updates:
for example how to check that I am in New Delhi city
You can try using LatLngBounds and LatLngBounds.contains()
private LatLngBounds NewDelhi = new LatLngBounds(your special area SE LatLng , NE LatLng );
if(NewDelhi.contains(your location LatLng))
//Do something
Get city name from Location:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(myLocation.getLatitude(), myLocation.getLongitude(), 1);
String cityName = addresses.get(0).getAddressLine(0);
//String stateName = addresses.get(0).getAddressLine(1);
//String countryName = addresses.get(0).getAddressLine(2);
For more: Displaying a Location Address

Getting current location in textview

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

Restrict Android user location within city perimeters

I'm building an app where I need to apply bounds on user's location (Lat,Lng) to be within city perimeters like Uber. How can I achieve this?
This one is when within city perimeters
This one is not in city or Uber's service zone.
I have tried this approach. I'm using geocoder and fetching the address of the location and matching city parameters.
addresses = geocoder.getFromLocation(lat, lon, 1);
if (addresses != null && !addresses.isEmpty()) {
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();
}
Can I use Geofencing? If yes then how?

Context Cannot be Resolved to a variable using gps

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.

Error in getting address of a location in android

based on this tutorial: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/. I can now get latitude and longtitude based on location. Now I', trying to get the exact address with Geocoder: (Main is the class code above belongs to)
GPSTracker GPS = new GPSTracker(Main.this);
double latitude = GPS.getLatitude();
double longitude = GPS.getLongitude();
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(Main.this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0);
LogCat says:
07-06 11:29:41.911: W/System.err(1670): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
I wonder what am I doing wrong?
GPSTracker GPS = new GPSTracker(Main.this);
double latitude = GPS.getLatitude();
double longitude = GPS.getLongitude();
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(Main.this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if(addresses!=null && addresses.size()!=0){
String address = addresses.get(0).getAddressLine(0);
}
//if(addresses!=null && addresses.size()!=0)...check this portion..your address cannot be null and and the the size of array should not be zero...

Categories

Resources