4

I am using Geocoder class to get Latitude and Longitude to from my String address that user types to EditText.

And what is interesting it returns results for such query as "q", "qq", "n", "N". Is there any way to make it better?(validate or something, 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();
                        }
Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
Kyryl Zotov
  • 1,788
  • 5
  • 24
  • 44

1 Answers1

0

You could always use another Service directly, or you could prune your results for the detail level you want.

So like:

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
      ...
}
Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64
  • Actually the address "q" has latitude and longitude. – Kyryl Zotov Sep 14 '15 at 07:52
  • Right but does it have other qualities. Lat and Long are easy. You can just pick a pair and it will cover enough area that it doesn't matter. But if you can actually get enough detailed address info then it might be a bit more what you are looking for. My point was more so add your own filtering based on your needs. – Greg Giacovelli Sep 14 '15 at 15:57
  • Yeah, if you would have some spare time try query places from my question. You would have street, city, even street number! – Kyryl Zotov Sep 15 '15 at 13:39