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();
} }
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 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
I want to do an application which gets the current address; country name, city name and district name. And then I have 3 spinner which are having all country names, city names and district names in Turkish.
When I get the address using GPS and Geocoder, I want to match them with the names in my spinners.
The problem is the address coming from geocoder is in English. And so, it does not match with my countries in my spinner. So I cannot select true country with programatically.
Here is my related code:
//In here I add countries coming from web service into countries array.
for(int i=0;i<responseInfo.size();i++) {
String name = responseInfo.get(i).getName();
countries.add(name);
//If country name is equals to country name coming from gps
//then I hold it in selectedCountry variable.
//But "Turkey" != "Türkiye" so this if block does not work
if(name.equalsIgnoreCase(foundAddress.getCountryName()))
{
selectedCountryId = (String)responseInfo.get(i).getId();
selectedCountry = responseInfo.get(i);
}
}
//In this method, when I get country name from the item in addresses array
//it returns in English
public List<Address> findAddressFromLatLng(double latitude, double longitude)
{
Geocoder geocoder;
List<Address> addresses = null;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if ((addresses != null) && (addresses.size() != 0)) {
return addresses;
}
return null;
}
In another device, I have no problem. Country name coming from geocoder is in Turkish (which means Geocoder have Turkish names also). Probably it is because of device language.
I tried to change this line but I can't find Turkish as locale:
geocoder = new Geocoder(this, Locale.getDefault());
Thanks in advance.
Docs for Geocoder suggest that setting a Locale is exactly what you need. There's no predefined Locale for Turkish, but you can easily define one:
Locale locale = new Locale("tr", "TR");
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;
}
The Android API has Location.distanceBetween(), which accepts two lat/lon values and returns a distance in meters. Is there a way that I could get this distance with only having a zip (postal) code for one of my points?
You may want to use Android's Geocoder API. Something like this should work:
String locationName = zipCode + ", " + countryName;
Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
try {
List<Address> address = geoCoder.getFromLocationName(locationName, 1);
double latitude = address.get(0).getLatitude();
double longitude = address.get(0).getLongitude();
Location.distanceBetween(...);
} catch (IOException e) {
e.printStackTrace();
}
You need to include the country's name because of this: Get latitude and longitude based on zip using Geocoder class in Android