How to get a country, city from a place to choose a place?

I use the goal of choosing a place to get a place. Now I want to save the address in a separate form as a country, city, pincode, state. How can I get all of this from a place picker?

code:

public class NameOfBusinessFragment extends Fragment { int PLACE_PICKER_REQUEST = 1; int RESULT_OK = -1; PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder(); EditText category,location; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_name_of_business, container, false); location = (EditText)view.findViewById(R.id.location); location.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST); } catch (GooglePlayServicesRepairableException e) { Toast.makeText(getActivity(),"ServiceRepaire Exception",Toast.LENGTH_SHORT).show(); } catch (GooglePlayServicesNotAvailableException e) { Toast.makeText(getActivity(),"SeerviceNotAvailable Exception",Toast.LENGTH_SHORT).show(); } } }); return view; } public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PLACE_PICKER_REQUEST) { if (resultCode == RESULT_OK) { Place place = PlacePicker.getPlace(data, getActivity()); String toastMsg = String.format("Place: %s", place.getName()); Toast.makeText(getActivity(), toastMsg, Toast.LENGTH_LONG).show(); location.setText(place.getName()); } } } } 

This is how I implemented the place collector and got the place name onActivityResult. Can I get this using reverse geocode or something else? Can someone help me with this please? Thanks.

+5
source share
3 answers

I think it is not possible to use the Place class directly, but you can set it to google, starting with latitude and longitude. The following is an example of your onActivityResult method:

 public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PLACE_PICKER_REQUEST) { if (resultCode == RESULT_OK) { // get place data Place place = PlacePicker.getPlace(data, getActivity()); // ask for geolocation data Geocoder gcd = new Geocoder(this, Locale.getDefault()); List<Address> addresses = null; try { addresses = gcd.getFromLocation(place.getLatLng().latitude, place.getLatLng().longitude, 1); } catch (IOException e) { e.printStackTrace(); } if (addresses.size() > 0) { String toastMsg = String.format("Place: %s", addresses.get(0).getLocality() + " - " + addresses.get(0).getCountryName() + " - " + addresses.get(0).getCountryCode()); Toast.makeText(getActivity(), toastMsg, Toast.LENGTH_LONG).show(); // NOW SET HERE CORRECT DATA //location.setText(place.getName()); } } } } 
+2
source

The best way to solve your problem is to use Geocoder to get the address from latitude and longitude, but if for some reason, if you don't want to use it, a workaround may help you.

Although it is not 100% reliable , you can use it until Google provides this detailed information. This may not give an ideal result for some random cases, but it works for most.

  public void getAddressDetails(Place place) { if (place.getAddress() != null) { String[] addressSlice = place.getAddress().toString().split(", "); country = addressSlice[addressSlice.length - 1]; if (addressSlice.length > 1) { String[] stateAndPostalCode = addressSlice[addressSlice.length - 2].split(" "); if (stateAndPostalCode.length > 1) { postalCode = stateAndPostalCode[stateAndPostalCode.length - 1]; state = ""; for (int i = 0; i < stateAndPostalCode.length - 1; i++) { state += (i == 0 ? "" : " ") + stateAndPostalCode[i]; } } else { state = stateAndPostalCode[stateAndPostalCode.length - 1]; } } if (addressSlice.length > 2) city = addressSlice[addressSlice.length - 3]; if (addressSlice.length == 4) stAddress1 = addressSlice[0]; else if (addressSlice.length > 3) { stAddress2 = addressSlice[addressSlice.length - 4]; stAddress1 = ""; for (int i = 0; i < addressSlice.length - 4; i++) { stAddress1 += (i == 0 ? "" : ", ") + addressSlice[i]; } } } if(place.getLatLng()!=null) { latitude = "" + place.getLatLng().latitude; longitude = "" + place.getLatLng().longitude; } } 
0
source

If you want the address components of the Place object (street, city, country, etc.) to be separated, you have two options.

  • Use Place.getLatLng() . Then, change the geocoding in latitude and longitude.
  • Sort the address.

Now parsing the address is not very simple. But reverse geocoding of latitude and longitude is inaccurate. I suggest parsing the address. There are good address parsing services, and some even check the address against address databases. (I suggest SmartyStreets . If you go to the SmartyStreets demo , select "Freeform address" from the drop-down list, and then see what information is returned when searching for the address.)


This is why reverse geocoding can be a bad solution. When you change the geocode, you select the latitude and longitude and algorithmically match it with the address. Some algorithms correspond to the latitude and longitude of the nearest valid address. In this case, the address may not be the address of your location. On the other hand, some algorithms approximate an address that matches latitude and longitude. In this case, the address may not be the real address. Another complication is mentioned in the documentation for getLatLng() :

The location is not necessarily the center of the place, or any specific point of entry or exit, but some arbitrary point within the geographical extent of the place.

Since latitude and longitude are arbitrary points in the geographical extent of a place, it is difficult to make reliable reverse geocoding algorithms.

But you may not like the accuracy. If so, and you need an address that is β€œclose enough”, reverse geocoding may be a good choice.


One more note:

static Place getPlace (intent intent, context context) This method is deprecated. Use getPlace (Context, Intent) instead.

I think you should replace your line:

 Place place = PlacePicker.getPlace(data, getActivity()); 

with this:

 Place place = PlacePicker.getPlace(getActivity(), data); 

Full disclosure: I work for SmartyStreets.

0
source

All Articles