Search code examples
androidgoogle-geocodergoogle-geocoding-api

Geocoder does not work for certain addresses


I am using the goecoder to try to convert address to lat/lng. However, it seems the geocoder works for some addresses but not others. If I search the address on google maps then it pinpoint it perfectly.

Here is an example 38 Crichton Street, Ottawa, ON, Canada This address returns 0 results when I run the code below

            Location loc = null;
            Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
            try {

                List<Address> address = geoCoder.getFromLocationName(addr, 1);
                if (address.size() == 0) {
                    return null;
                }
                double latitude = address.get(0).getLatitude();
                double longitude = address.get(0).getLongitude();
                loc = new Location("Unknown");
                loc.setLatitude(latitude);
                loc.setLongitude(longitude);
            }  catch (IOException ioException) {
                Log.e(TAG, ioException.toString());
            } catch (IllegalArgumentException illegalArgumentException) {
                Log.e(TAG, illegalArgumentException.toString());
            }

Any idea why or how I can fix it

Thank you


Solution

  • Not sure why native Android API geocoder cannot find this address. I can see that this address is found in Geocoding API web service:

    https://maps.googleapis.com/maps/api/geocode/json?address=38%20Crichton%20Street%2C%20Ottawa%2C%20ON%2C%20Canada&key=MY_API_KEY

    You can also see it in Geocoder tool:

    https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D38%2520Crichton%2520Street%252C%2520Ottawa%252C%2520ON%252C%2520Canada

    As a workaround you can consider using the Geocoding API web service. Please note that there is a Java client library for web services that you can find on Github:

    https://github.com/googlemaps/google-maps-services-java

    The Javadoc for client library is located at

    https://googlemaps.github.io/google-maps-services-java/v0.2.5/javadoc/

    I hope this helps!