How can I get values from address of Place Picker? - android

I want to get values from address of Place Picker in android and separate it into different EditText like I want to set pincode on PineCodeEditText, set country on CountryEditText, set State on StateEditText and so on. I have got address as
10, Mothorowala Rd, Jagdamba Colony, Dharmpur, Ajabpur Kalan,
Dehradun, Uttarakhand 248001, India
Where I used
Place place = PlacePicker.getPlace(data,this);
String address = String.format(String.valueOf(place.getAddress()));
Before this I used Split function but it gives indexOutOfBoundException.
Any suggestion please welcome, Thank You.

You can get the latitude and longitude coordinates from the Place object, then get the address object using the GeoCoder, which you can use to get the required data separately.
LatLng coordinates = place.getLatLng(); // Get the coordinates from your place
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(
coordinates.latitude,
coordinates.longitude,
1); // Only retrieve 1 address
Address address = addresses.get(0);
String countryCode = address.getCountryCode();
String countryName = address.getCountryName();

Related

Geocoder not returning Address of some loction

I have the issue with some location that donot return me the address while other returns me the address i have issue with the following address
List addresses = geocoder.getFromLocationName(
"568 S. Coy St., Kansas City, Kansas, 66105", 1);
List addresses = geocoder.getFromLocationName(
"10075 Bauer Rd., St. Louis, Missouri, 63128", 1);
I just try to get the Address with the hardcode string but i got failure to get the address , While if i remove the initial address it return me the address of location while if i put this on the web page of google map it shows me the marker on the actual location where the place is.

I want the street name and address with logitute and latitude as shown in image in android

I want the street name and longitude latitude of entered pin code in edit text as shown in the image of google map. I want to develop this type of feature in android.
Thank you in advance.
Try this,
Geocoder geo = new Geocoder(getApplicationContext(),
Locale.getDefault());
List<Address> add;
try {
add = geo.getFromLocation(taplat, taplon, 1);
addstr = add.get(0).getPostalCode();
}

Latitude and longitude addresses for Google Maps always show Chinese locations

The Problem
I'm trying to retrieve latitude and longitude from an address using Geocoder.getFromLocationName().
It works perfectly for some Android devices, but it keeps showing Chinese latitude and longitude addresses for my Xiamoi mobile phone. However, it should be showing my location - Saket, Delhi, India.
Here's the code:
public LatLng getLocationFromAddress(String strAddress) {
Geocoder coder = new Geocoder(getActivity(), Locale.getDefault());
List<Address> address;
LatLng p1 = null;
try {
address = coder.getFromLocationName(strAddress, 5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new LatLng(location.getLatitude(), location.getLongitude());
return p1;
}
}
Change the Locale
The Locale passed into the Geocoder constructor represents the desired Locale for the query results. This applies the the following code:
Geocoder coder = new Geocoder(getActivity(), Locale.getDefault());
Be wary of the default locale
From the documentation for Locale.getDefault();
Note that there are many convenience methods that automatically use the default locale, but using them may lead to subtle bugs.
A common mistake is to implicitly use the default locale when producing output meant to be machine-readable. This tends to work on the developer's test devices (especially because so many developers use en_US), but fails when run on a device whose user is in a more complex locale.
The default Locale is most likely China. This makes sense seeing as Xiaomi is a Chinese manufacturer. This is probably why you keep getting Chinese addresses.
Test
Try the following and check your logs to see what the locale is being set to:
Locale defaultLocale = Locale.getDefault();
Log.i("MyActivity", defaultLocale.toString());

Android Geocoder class, missing Town and Postcode

I have problem with geocoding on android device.
Geocoder c = new Geocoder(this, Locale.getDefault());
List<Address> l = c.getFromLocation(lat, lon, 1);
for (Address a: l) {
Log.i("GeocoderResult", "Locality " + a.getLocality() + " poscode " + a.getPostcode());
}
The Android Geocoder returns the android.location.Address object. The text returned can have city and postcode in it, but corresponding object fields may still be empty.
Geocoding with town and postcode in the text, but not in the object fields:
50.74967518;-1.99207054:
Address text: "17 Gough Crescent, Poole, BH17, GB"
locality = null
postcode = null
Address[addressLines=[0:"17 Gough Crescent",1:"Poole",2:"BH17",3:"UK"],feature=17,admin=Poole,sub-admin=Poole,locality=null,thoroughfare=Gough Crescent,postalCode=null,countryCode=GB,countryName=United Kingdom,hasLatitude=true,latitude=50.7498262,hasLongitude=true,longitude=-1.992054,phone=null,url=null,extras=null]
Geocoding with town and postcode both in the text and in the object fields:
50.74966423;-1.99205967
Address text: 2-6 Gough Crescent, Poole BH17, GB
locality = Poole
postalCode=BH17
Address[addressLines=[0:"2-6 Gough Crescent",1:"Poole BH17",2:"UK"],feature=2-6,admin=null,sub-admin=null,locality=Poole,thoroughfare=Gough Crescent,postalCode=BH17,countryCode=GB,countryName=United Kingdom,hasLatitude=true,latitude=50.7496887,hasLongitude=true,longitude=-1.9917892,phone=null,url=null,extras=null]
Are we missing something and something can be done to retrieve the missing town and postcode data using just Android Geocoder as is? Or is the only way to do this guessing which of the fields is likely to contain town data if the 'locality' is empty and trying to parse address text for postcode?

Reverse Geocoding issue

I implemented the reverse geocoding in my app and it is working, but sometimes it happens a very strange issue.
The code is that
List<Address> addresses = geo.getFromLocation(
obj.getLatitude(), obj.getLongitude(), 1);
List<Address> address = geo.getFromLocationName( addresses.get(0).getLocality().getBytes() , 1 );
Address location = address.get(0);
In the first part I get the address object of the place in which I'm. Than I want recover the generic coordinates of the city in where I'm because I don't want store the coordinates of my exact position.
This is working but I encounter a very strange issue! Trying the app with the fakegps app I set my position in "Ñuñoa", and the first address was found correctly, but when I try to get the generic coordinates, I get "Nunoa" that isn't in Chile, but in Peru!!
That makes no sense! why this?
Thanks for helping me
Sure Swathi.
List<Address> addresses = geo.getFromLocation(msg_r.getLatitude(),
msg_r.getLongitude(), 1);
String geoL = addresses.get(0).getLocality() + ", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryCode();
// reverse-reverseGeocoding
List<Address> address = geo.getFromLocationName(geoL, 1);
System.out.println("Where I am? " + geoL);
Address location = address.get(0);
// generic coordinate for the locality/city/town
location.getLatitude();
location.getLongitude();

Categories

Resources