My Question is i have an Activity of multiple EditText like Street,landmark,Location,City etc. On the Location EditText i put a button to fetch the Current User Location and Fill the User Address in Respective EditText.
NOTE : i Successfully Fetch my current location and populate my location EditText whole but i want to add street Number in Street EditText, City in City EditText etc
You can use Official Documentation for beginning and find many examples of "reverse geocoding" like this:
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(); //
from here
Related
Below is my code, I am getting null subLocality always ....
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude,longitude, 1);
String suburb = addresses.get(0).getSubLocality();
String state = addresses.get(0).getAdminArea();
String zip = addresses.get(0).getPostalCode();
String country = addresses.get(0).getCountryName();
Log.e("Log for data",""+suburb+" "+state+" "+zip+" "+country);
Below is my response;
null Gujarat 380006 India
Geocoder API will return the values depending on lat long, if the API database doesn't have these values exemple : subLocality then it will return null only, in my knowledge there's nothing you can do about it. You have to handle these cases gracefully so your app don't crash.
Handling these cases includes you have to put a check using if condition is that particular value is null or it has some value, because if you use any of the value somewhere in code without checking and if that value is null then app might crash whenever that particular value comes null.
I too had the same problem which was solved by increasing the number of results which you request from Geocoder API.
Try increasing it to 10
List<Address> addresses = geocoder.getFromLocation(latitude,longitude, 10);
Then if your sub-locality is null with the first result, try search in the rest of them for the same
I am using the autocomplete widget of Google Place API for Android and getting a place object, but I am not able to extract the city of it, just only the complete address as a text.
Apparently, the Place interface does not have a method to return the city.
The city is the address componenets is Locality and Administrative Area Level 3, according this documentation.
Does someone knows any way to get the city of the Place object ?
It is a hack but it works. You can take the lat and lng from the Place object and find the city from the Geocoder.
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
String cityName = addresses.get(0).getAddressLine(0);
String stateName = addresses.get(0).getAddressLine(1);
String countryName = addresses.get(0).getAddressLine(2);
In Bangladesh there are 65 Districts (ex: Dhaka, Chittagong etc). I want to add a feature in my Android App. After pressing the particular button user get his District name in a TextView.
We can easily mark the current location of user in Google map. But I need the District name as a String.
How can it be possible?
Thanks in advance... :)
From a Geocoder object, you can call the getFromLocation(double, double, int) method. It will return a list of Address objects that have a method getLocality().
Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0)
Log.e("District",addresses.get(0).getLocality());
The following is my method:
List<Address> addresses = null;
addresses = new Geocoder(passengerHomeActivity).getFromLocationName(this.addressInput.getText().toString(), 1);
is there any way to limit the Geocoder getFromLocationName's scope to one specific city? Sometimes when I enter an Address without writing the city name, it leads me to an address which is the same but in another city.
My idea is by getting the latitude and longitude of the user, get the value of the city, and set the limit scope to the getFromLocationName method, but I don't have any clue with the implementation.
Thanks
Use this code:
List<Address> addresses = new Geocoder(passengerHomeActivity).getFromLocationName((this.addressInput.getText().toString()) + ", YourCity, YourCountry", 1);`
Use the constructor passing the the Locale.
Geocoder(Context context, Locale locale)
Use it like this:
Locale locale = new Locale(Locale.getDefault().getLanguage(), "US");
Geocoder geo = new Geocoder(context, locale)
addresses = geo.getFromLocationName(term, 1);
Notice that this is an aproach, because it will give you the result in that country unless nothing is found (in which case will return worldwide anyway).
In Google Maps how can we get Latitude and Longitude by passing the Address?
and what is the Address format need to be mentioned?
To find latitude and longitude from address string .....
Make object of Geocoder
Call getFromLocationName() pass the address (place name) string as argument also maximum number of results you want for that place name
Here is demo ...
Geocoder coder = new Geocoder(LocationMap.this);
List<Address> geocodeResults = coder.getFromLocationName(placeName, 10);
Iterator<Address> locations = geocodeResults.iterator();
while (locations.hasNext()) {
Address loc = locations.next();
_lattitude_ = loc.getLatitude() ;
_longidude_ = loc.getLongitude();
}
And there are no specific address format required.
For example you can try any city name, country name, hotel name, area name, road name, well known place name ( Try "Eiffel tower") ... anything (from many things).
Found this older post. Should solve your problem.