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...
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
Right now I currently am displaying the Latitude/Longitude within my Android Studio MapsActivity Marker by using the following:
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(String.valueOf(latLng));
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
currentLocationMarker = mMap.addMarker((markerOptions));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
My goal is to display the entire address.
Use geocoder to get address from latlng
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();
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();
} }
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;
}
I am new to android mobile development. I have used the Location Manager class and successfully found out the Longitude and the Latitude of the user. I want to use these values to find the city name. I don't want maps, I just want to get the city name. How do I do this?
First get Latitude and Longitude using Location and LocationManager class(That you have completed). Now try the code below for Get the city,address info
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
StringBuilder 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)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
City info is now in sb. Now convert the sb to String (using sb.toString() ).
https://github.com/commonsguy/cw-lunchlist
https://github.com/commonsguy/cw-android
http://developer.android.com/reference/android/location/LocationManager.html
Have a look at these sites this will help you!!!!!1
You can use the Geocoder
Geocoder myLocation = new Geocoder(context, Locale.getDefault());
List<Address> myList = null;
try {
myList = myLocation.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {}
Where longitude and latitude are the valued retrieved by networks or GPS