Geocoder returned address

I use the class Geocoderto get the Latitude and Longitude value from my string address, which the user dials to EditText.

And interestingly, it returns results for a query such as "q", "qq", "n", "N". Is there any way to make this better? (Confirm or something else or use another service?)

if (!s.toString().isEmpty()) {
          try {
                 List<Address> addresses = geocoder.getFromLocationName(s.toString(), 4);
                            if (addresses.size() > 0) {
                                Address userAddress = addresses.get(0);
                                double latitude = userAddress.getLatitude();
                                double longitude = userAddress.getLongitude();
                                mRestaurantLatLng = new LatLng(latitude, longitude);
                                if (userAddress != null) {
                                    mPin.setVisibility(View.VISIBLE);
                                } else {
                                    mPin.setVisibility(View.GONE);
                                    mRestaurantLatLng = null;
                                }
                            } else {
                                mPin.setVisibility(View.GONE);
                                mRestaurantLatLng = null;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
+4
source share
1 answer

You can always use another service directly, or you can crop the results for the required level of detail.

So how:

List<Address> results = GeoCoder.getFromLocationName(query, 100);
filter(results);
if (results.size() > 0) {
   // Do Stuff
}


....

void filter(List<Address> results) {
   for (int i = 0; i < results.size(); i++) {
      Address address = results.get(i);
      if (!address.hasLatitude() || !address.hasLongitude()) {
          results.remove(i);
      }
      // remove any that dont match your desired detail level
      ...
}
0
source

Source: https://habr.com/ru/post/1606989/


All Articles