Android GeoCoder: zip code search returns unexpected results

I want to perform a search by entering user text using the GeoCoder class from Android. Here is my code:

Geocoder iGeocoder = new Geocoder(getContext(), Locale.GERMAN);

public Address getAdress(String aUserInput) {
    List<Address> tAddressList = iGeocoder.getFromLocationName(aUserInput, 1000, 47.060940, 8.564278, 51.526396, 13.736392);
    if(tAddressList != null &&
                tAddressList.size() > 0) {
        for(Address tAddress : tAddressList) {

            // return the first adress found for germany.
            Log.e(TAG, "returning Adress: " + tAddress );
            if(tAddress.getCountryCode().equals("DE")) {

                return tAddress;
            }

        }
        return null;
    } else {
        return null;
    }
}

The restrictive box that I pass in getFromLocationNameroughly represents the German state of Bavaria. I know that this bounding box does not guarantee that the resulting address really exists. I found some problems, especially when searching by zip code.

  • Some of the resulting addresses returned when searching by zip code do not have an "admin zone", so I cannot filter for my desired state (= "Bavaria").
  • , 97070, 97078 97076, GeoCoder , . . . 97082 , 97070 ( ) , .
+4
1

locale. . Germany .

Locale locale = new Locale(langCode, countryCode) 

Locale locale = new Locale("de", "DE");

Geocoder iGeocoder = new Geocoder(getContext(), locale);

GeoCode , , . :

97070 Wilsonville, OR 97070, USA

47.060940, 8.564278 Spitzibüelstrasse 8, 6410 Goldau, Switzerland

97070, 47.060940, 8.564278 ( ), , .

, .

- null if , .

public Address getAdress(String aUserInput) {
    .../...
    if(.../...) {
        for(Address tAddress : tAddressList) {
            .../...
            if(tAddress.getCountryCode().equals("DE")) {

                return tAddress;
            }
        }
     }
    return null;
}

Region Biasing jausen brett

https://maps.googleapis.com/maps/api/geocode/json?address=<some address>&region=de&key=YOUR_API_KEY

https://maps.googleapis.com/maps/api/geocode/json?components=locality:<desired locality>|country:DE&key=YOUR_API_KEY
+8

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


All Articles