aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/geocoder
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/geocoder')
-rw-r--r--src/org/traccar/geocoder/Geocoder.java4
-rw-r--r--src/org/traccar/geocoder/JsonGeocoder.java95
2 files changed, 45 insertions, 54 deletions
diff --git a/src/org/traccar/geocoder/Geocoder.java b/src/org/traccar/geocoder/Geocoder.java
index c3fa394c7..587a27520 100644
--- a/src/org/traccar/geocoder/Geocoder.java
+++ b/src/org/traccar/geocoder/Geocoder.java
@@ -25,8 +25,6 @@ public interface Geocoder {
}
- void getAddress(double latitude, double longitude, ReverseGeocoderCallback callback);
-
- String getAddress(double latitude, double longitude);
+ String getAddress(double latitude, double longitude, ReverseGeocoderCallback callback);
}
diff --git a/src/org/traccar/geocoder/JsonGeocoder.java b/src/org/traccar/geocoder/JsonGeocoder.java
index 2ae1fe5aa..82a6ee604 100644
--- a/src/org/traccar/geocoder/JsonGeocoder.java
+++ b/src/org/traccar/geocoder/JsonGeocoder.java
@@ -28,7 +28,6 @@ import java.io.IOException;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.LinkedHashMap;
-import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ExecutionException;
@@ -52,70 +51,64 @@ public abstract class JsonGeocoder implements Geocoder {
}
}
- @Override
- public void getAddress(
- final double latitude, final double longitude, final ReverseGeocoderCallback callback) {
-
- if (cache != null) {
- String cachedAddress = cache.get(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude));
- if (cachedAddress != null) {
- callback.onSuccess(cachedAddress);
- return;
- }
- }
-
- Context.getAsyncHttpClient().prepareGet(String.format(Locale.US, url, latitude, longitude))
- .execute(new AsyncCompletionHandler() {
- @Override
- public Object onCompleted(Response response) throws Exception {
- try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) {
- Address address = parseAddress(reader.readObject());
- if (address != null) {
- String formattedAddress = addressFormat.format(address);
- if (cache != null) {
- cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress);
- }
- callback.onSuccess(formattedAddress);
- } else {
- callback.onFailure(new GeocoderException("Empty address"));
- }
+ private String handleResponse(double latitude, double longitude, Response response,
+ ReverseGeocoderCallback callback) throws IOException {
+ try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) {
+ Address address = parseAddress(reader.readObject());
+ if (address != null) {
+ String formattedAddress = addressFormat.format(address);
+ if (cache != null) {
+ cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress);
}
- return null;
- }
-
- @Override
- public void onThrowable(Throwable t) {
- callback.onFailure(t);
+ if (callback != null) {
+ callback.onSuccess(formattedAddress);
+ }
+ return formattedAddress;
+ } else {
+ if (callback != null) {
+ callback.onFailure(new GeocoderException("Empty address"));
+ }
+ Log.warning("Empty address");
}
- });
+ }
+ return null;
}
@Override
- public String getAddress(final double latitude, final double longitude) {
+ public String getAddress(
+ final double latitude, final double longitude, final ReverseGeocoderCallback callback) {
+
if (cache != null) {
String cachedAddress = cache.get(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude));
if (cachedAddress != null) {
+ if (callback != null) {
+ callback.onSuccess(cachedAddress);
+ }
return cachedAddress;
}
}
- try {
- Response response = Context.getAsyncHttpClient()
- .prepareGet(String.format(Locale.US, url, latitude, longitude)).execute().get();
- try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) {
- Address address = parseAddress(reader.readObject());
- if (address != null) {
- String formattedAddress = addressFormat.format(address);
- if (cache != null) {
- cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress);
- }
- return formattedAddress;
- } else {
- Log.warning("Empty address");
+ if (callback != null) {
+ Context.getAsyncHttpClient().prepareGet(String.format(url, latitude, longitude))
+ .execute(new AsyncCompletionHandler() {
+ @Override
+ public Object onCompleted(Response response) throws Exception {
+ return handleResponse(latitude, longitude, response, callback);
}
+
+ @Override
+ public void onThrowable(Throwable t) {
+ callback.onFailure(t);
+ }
+ });
+ } else {
+ try {
+ Response response = Context.getAsyncHttpClient()
+ .prepareGet(String.format(url, latitude, longitude)).execute().get();
+ return handleResponse(latitude, longitude, response, null);
+ } catch (InterruptedException | ExecutionException | IOException error) {
+ Log.warning("Geocoding failed", error);
}
- } catch (InterruptedException | ExecutionException | IOException error) {
- Log.warning("Geocoding failed", error);
}
return null;
}