Search in sources :

Example 1 with GeocoderRequestBuilder

use of com.google.code.geocoder.GeocoderRequestBuilder in project opennms by OpenNMS.

the class GoogleGeocoderService method getCoordinates.

@Override
public synchronized Coordinates getCoordinates(final String address) throws GeocoderException {
    ensureInitialized();
    final GeocoderRequest request = new GeocoderRequestBuilder().setAddress(address).setLanguage("en").getGeocoderRequest();
    GeocodeResponse response;
    try {
        response = m_geocoder.geocode(request);
    } catch (IOException e) {
        // Makes the assumption that IO related exceptions are temporary, which is suitable for most scenarios
        throw new TemporaryGeocoderException("Failed to get coordinates for " + address + " using the Google Geocoder.", e);
    }
    switch(response.getStatus()) {
        case OK:
            return new GoogleCoordinates(response.getResults().get(0));
        case OVER_QUERY_LIMIT:
            throw new TemporaryGeocoderException("Failed to get coordinates for " + address + " using the Google Geocoder.  You have exceeded the daily usage limit.");
        case ERROR:
        case INVALID_REQUEST:
        case REQUEST_DENIED:
        case UNKNOWN_ERROR:
        case ZERO_RESULTS:
        default:
            throw new GeocoderException("Failed to get coordinates for " + address + " using Google Geocoder.  Response was: " + response.getStatus().toString());
    }
}
Also used : GeocoderRequestBuilder(com.google.code.geocoder.GeocoderRequestBuilder) TemporaryGeocoderException(org.opennms.features.geocoder.TemporaryGeocoderException) GeocodeResponse(com.google.code.geocoder.model.GeocodeResponse) IOException(java.io.IOException) GeocoderException(org.opennms.features.geocoder.GeocoderException) TemporaryGeocoderException(org.opennms.features.geocoder.TemporaryGeocoderException) GeocoderRequest(com.google.code.geocoder.model.GeocoderRequest)

Aggregations

GeocoderRequestBuilder (com.google.code.geocoder.GeocoderRequestBuilder)1 GeocodeResponse (com.google.code.geocoder.model.GeocodeResponse)1 GeocoderRequest (com.google.code.geocoder.model.GeocoderRequest)1 IOException (java.io.IOException)1 GeocoderException (org.opennms.features.geocoder.GeocoderException)1 TemporaryGeocoderException (org.opennms.features.geocoder.TemporaryGeocoderException)1