"How to get actual text value(Place name) from LatLong ?" - android

I am working on GoogleMap in Android.
So far, I have got the current location and displayed marker on it.
I got LatLong value from my current location.
I am able to get city name using following code :
Geocoder gcd = new Geocoder(MainActivity.this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation( mLocation.getLatitude(), mLocation.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses.size() > 0)
System.out.println(addresses.get(0).getLocality());
Toast.makeText(MainActivity.this,""+addresses.get(0).getLocality(),Toast.LENGTH_LONG).show();
}
But, can't get the atual area name i.e. Bodakdev char rasta, Ahmedabad.
Now, my question is: How to get actual text value (Place name) from LatLong?

You can get this by Geocoder object in your google map. The method getFromLocation(double, double, int) does the work.
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1); // 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();

Use this,
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);
String address = addresses.get(0).getAddressLine(0);

You can also get city name from zip-Code or postal-code of the place
private fun getLatLngByZipcode(zipcode: String): String {
var place = context.getString(R.string.location)
val geocoder = Geocoder(context, Locale.getDefault())
try {
val addresses = geocoder.getFromLocationName(zipcode, 5)
addresses?.forEach{
it?.locality?.apply {
place=this
}
}
}
catch (e: IOException) {
LogUtils.error(TAG,e.message)
}
return place
}

Related

How to get city and state name in Autocomplete Places _ Places API

Iam using Places Autocomplete for selecting city from user, its working fine,
But now i want both city and state name..
my code..
Initialising
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
Intent intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.OVERLAY, fields)
.setTypeFilter(TypeFilter.CITIES)
.setCountry("IN")
.build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
onActivity Result
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//Place place = Autocomplete.getPlaceFromIntent(data);
Place place = Autocomplete.getPlaceFromIntent(data);
edit_profile_city_editText.setText(place.getName());
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
Status status = Autocomplete.getStatusFromIntent(data);
Log.i("Autocomplete error", status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
}
}
While selecting city from search bar its showing both city and state..
eg:Chennai
TamilNadu, India
Kindly help how to get state name also...
Use like this:-
and onActivityResult
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
LatLng latLng = place.getLatLng();
double MyLat = latLng.latitude;
double MyLong = latLng.longitude;
Geocoder geocoder = new Geocoder(EditProfileActivity.this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
String stateName = addresses.get(0).getAdminArea();
String cityName = addresses.get(0).getLocality();
edit_profile_city_editText.setText(place.getName() + "," + stateName);
} catch (IOException e) {
e.printStackTrace();
}
Hope this will help you.Thanks..
Try something like this:
Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault());
List<Address> addresses = new ArrayList<>();
addresses = geocoder.getFromLocation(lat, lng, 1);
String country = addresses.get(0).getCountryName();
I think these will work for you;
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);
or
String address = addresses.get(0).getSubLocality();
String cityName = addresses.get(0).getLocality();
String stateName = addresses.get(0).getAdminArea();

android geocoder : how to retrieve the name of the city in the local of the country of the city?

i use the android geocoder to retrieve the name of cities. however the name is returned in the local we use to construct the geocoder. is their any way to retrieve the name in the local of the city (exemple paris for paris in france or Москва for moscow in russia).
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());
}
else
{
// do your staff
}
try this you will get the complete address
private void getLocation(double lat, double lng) {
try {
Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
address.getAddressLine(0);
address.getAddressLine(1);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(address.getAddressLine(i)).append(" ");
}
Log.d(TAG, "strReturnedAddress : " + strReturnedAddress);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
You can use the Geocoder's getFromLocation method:
final LatLng lLatLng = new LatLng(0.0, 0.0);
final LatLng lMaxResults = 10;
final Geocoder lGeocoder = new Geocoder(this, Locale.ENGLISH);
final List<Address> lAddresses = lGeocoder.getFromLocation(lLatLng.latitude, lLatLng.longitude, lMaxResults);

Get (ONLY) the city name from coordinates in Android

As the title writes, I would like to get the name of city (only) and not the address from coordinates. I find the code below but it returns me all the address and not only the city.
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
StringBuilder builder = new StringBuilder();
try {
List<Address> address = geoCoder.getFromLocation(latitude, longitude, 1);
int maxLines = address.get(0).getMaxAddressLineIndex();
for (int i=0; i<maxLines; i++) {
String addressStr = address.get(0).getAddressLine(i);
builder.append(addressStr);
builder.append(" ");
}
String finalAddress = builder.toString(); //This is the complete address.
} catch (IOException e) {}
catch (NullPointerException e) {}
Please if someone could help me I'll be very pleasure.
Thank's a lot in advance!
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
StringBuilder builder = new StringBuilder();
try {
List<Address> address = geoCoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
String city = addresses.get(0).getLocality();
}
//the rest of your code
I don't think there is a better way to get the City name ONLY, except using getLocality(). When we use getLocality(), you can see that we have City name, country name etc. Just analyze the output of this function and you will encounter that all the bits are separated by "Comma". City name is at 0th index.
private void getLocation(String lat, String longt) {
String myCity = " ";
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
try {
List<Address>addresses = geocoder.getFromLocation(Double.parseDouble(lat),Double.parseDouble(longt), 1);
String address = addresses.get(0).getAddressLine(0);
myCity = addresses.get(0).getLocality();
Log.d("myLog", "Address: " + address);
String arrp[] = address.split(",");
Log.d("myLog", "City: " + arrp[0]);
}
catch (IOException e) {
e.printStackTrace();
}

How to get postal code using user current location in android

I am trying to get postal code, but I am unable to get zipcode(postalcode). I can able to get current city but when I try to get the zipcode it's giving me a null pointer exception.
Can anyone help me.
final Geocoder gcd = new Geocoder(getApplicationContext(),
Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) Log.d(addresses.get(0).getLocality()); // I can get city name here.
Log.d(addresses.get(0).getPostalCode();// here i am getting nullpoiter exception
Try to use android built-in Geocoder to get details from latitude and longitude without calling google location api as below :
Initialize Gecoder using Context :
final Geocoder gcd = new Geocoder(context);
Get Address as result from Lat-Long, Here (10) max result.
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 10);
Iterate to result get required location details :
for (Address address : addresses) {
if(address.getLocality()!=null && address.getPostalCode()!=null){
Log.d(address.getLocality());
Log.d(address.getPostalCode();
break;
}
}
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
Address address=null;
String addr="";
String zipcode="";
String city="";
String state="";
if (addresses != null && addresses.size() > 0){
addr=addresses.get(0).getAddressLine(0)+"," +addresses.get(0).getSubAdminArea();
city=addresses.get(0).getLocality();
state=addresses.get(0).getAdminArea();
for(int i=0 ;i<addresses.size();i++){
address = addresses.get(i);
if(address.getPostalCode()!=null){
zipcode=address.getPostalCode();
break;
}
}
I Used google webservice to get the zipcode.
The following is the google web service
http://maps.googleapis.com/maps/api/geocode/json?latlng=lattitude,longitude&sensor=true
here lattitude and longitude. so replace those values and you will get response and parse them and get postal code.
I have utility method to grab postcode which is pretty neat and works fine..
public static String getPostalCodeByCoordinates(Context context, double lat, double lon) throws IOException {
Geocoder mGeocoder = new Geocoder(context, Locale.getDefault());
String zipcode=null;
Address address=null;
if (mGeocoder != null) {
List<Address> addresses = mGeocoder.getFromLocation(lat, lon, 5);
if (addresses != null && addresses.size() > 0) {
for (int i = 0; i < addresses.size(); i++) {
address = addresses.get(i);
if (address.getPostalCode() != null) {
zipcode = address.getPostalCode();
Log.d(TAG, "Postal code: " + address.getPostalCode());
break;
}
}
return zipcode;
}
}
return null;
}

How to get latitude & longitude from address on Android? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I find the latitude and longitude from address?
I want to get latitude and longitude of particular address . How can i do this ?
private void getFromLocation(String address)
{
double latitude= 0.0, longtitude= 0.0;
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = geoCoder.getFromLocationName(address , 1);
if (addresses.size() > 0)
{
GeoPoint p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
latitude=p.getLatitudeE6()/1E6;
longtitude=p.getLongitudeE6()/1E6;
}
}
catch(Exception ee)
{
}
}
}
You can get from the below code,
private void GetLatitudeAndLongitude() {
geocoder = new Geocoder(mContext, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocationName(txtLocation.getText().toString().trim().toLowerCase(), 1);
if (addresses.size() > 0) {
homeInfoModel.setLalitude(String.valueOf(addresses.get(0).getLatitude()));
homeInfoModel.setLongitude(String.valueOf(addresses.get(0).getLongitude()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
This will give you (loop through) all the matches of the address string.
If you just desire the first match, make sure address.size() is great than 0, then take address.get(0);
In my usage, I get all matches, and show them as options. The user then clicks one, and it selects that GPS location.
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses;
try {
addresses = geocoder.getFromLocationName("Example StreeT, UK, DNFE", 20);
for(int i = 0; i < addresses.size(); i++) { // MULTIPLE MATCHES
Address addr = addresses.get(i);
double latitude = addr.getLatitude();
double longitude = addr.getLongitude(); // DO SOMETHING WITH VALUES
}
}

Categories

Resources