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();
Related
I am trying to get city name from Google place picker. is there any way that we can get city name using place picker API. i know we can not simply get it from API by placing place.getCity(); Below is my code.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_PLACE_PICKER){
if(resultCode==RESULT_OK){
Place place = PlacePicker.getPlace(data,this);
final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
final CharSequence phone = place.getPhoneNumber();
final String placeId = place.getId();
final LatLng latLng = place.getLatLng();
if(place.getLocale() != null) {
String aname = place.getLocale().toString();
areaname.setText(aname);
}
location.setText(address);
gname.setText(name);
latlon.setText(String.valueOf(latLng));
}
}
}
I don't want any null values in city. i read Geocoder will give mostly null values.
Try using geocoder
final Place place = PlacePicker.getPlace(this,data);
Geocoder geocoder = new Geocoder(this);
try
{
List<Address> addresses = geocoder.getFromLocation(place.getLatLng().latitude,place.getLatLng().longitude, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
//String country = addresses.get(0).getAddressLine(2);
} catch (IOException e)
{
e.printStackTrace();
}
Also you can try to call webservice and parse json result to get city.
Just pass the lat/ lng you get from placepicker in below url.
In the result administrative_area_level_2 represents the city
http://maps.googleapis.com/maps/api/geocode/json?latlng=23,72&sensor=true
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
}
I want to get location address from LatLng
I tried some ways but i did not get answer, because it seems this service closed by google, so i getting timeout error when i using following code, is there another solution?
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).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
private String getCompleteAddressString(double LATITUDE, double LONGITUDE) {
String strAdd = "";
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
strAdd = strReturnedAddress.toString();
Log.w("My Current loction address", "" + strReturnedAddress.toString());
} else {
Log.w("My Current loction address", "No Address returned!");
}
} catch (Exception e) {
e.printStackTrace();
Log.w("My Current loction address", "Canont get Address!");
}
return strAdd;
}
Try this function, it is working fine.
This is working fine, check code below and keep your geocoder.getFromLocation() method in try block
Click Here
Try this one
public static String getAddressInString(Context context, LatLng latLng) {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
if (addresses != null && addresses.size() > 0) {
return convertToString(addresses.get(0));
} else {
return "";
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static String convertToString(Address obj) {
String add = "";
if (obj == null)
return "";
add = obj.getAddressLine(0);
if (obj.getSubAdminArea() != null)
add = add + "\n" + obj.getSubAdminArea();
if (obj.getPostalCode() != null)
add = add + " - " + obj.getPostalCode();
if (obj.getAdminArea() != null)
add = add + "\n" + obj.getAdminArea();
if (obj.getCountryName() != null)
add = add + "\n" + obj.getCountryName();
return add;
}
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();
}
In my app I am using osm map. I have latitude and longitude.
using this method
proj = mapView.getProjection();
loc = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());
String longitude = Double
.toString(((double) loc.getLongitudeE6()) / 1000000);
String latitude = Double
.toString(((double) loc.getLatitudeE6()) / 1000000);
Toast toast = Toast.makeText(getApplicationContext(), "Longitude: "
+ longitude + " Latitude: " + latitude, Toast.LENGTH_SHORT);
toast.show();
So from here how I will query to get the city name from osm database. Please help me.
How can I convert this into human understandable form. Here is my code which I am using.link
Try this code for getting address.
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).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
for openstreammap
final String requestString = "http://nominatim.openstreetmap.org/reverse?format=json&lat=" +
Double.toString(lat) + "&lon=" + Double.toString(lon) + "&zoom=18&addressdetails=1";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(requestString));
try {
#SuppressWarnings("unused")
Request request = builder.sendRequest(null, new RequestCallback() {
#Override
public void onResponseReceived(Request request, Response response) {
if (response.getStatusCode() == 200) {
String city = "";
try {
JSONValue json = JSONParser.parseStrict(response);
JSONObject address = json.isObject().get("address").isObject();
final String quotes = "^\"|\"$";
if (address.get("city") != null) {
city = address.get("city").toString().replaceAll(quotes, "");
} else if (address.get("village") != null) {
city = address.get("village").toString().replaceAll(quotes, "");
}
} catch (Exception e) {
}
}
}
});
} catch (Exception e) {
}
here is my solution. i think it works for you also.
public String ConvertPointToLocation(GeoPoint point) {
String address = "";
Geocoder geoCoder = new Geocoder( getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
e.printStackTrace();
}
return address;
}