aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/geocoder/JsonGeocoder.java
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2017-11-10 09:56:30 +0500
committerAbyss777 <abyss@fox5.ru>2017-11-10 09:56:30 +0500
commit96d89e2e352f5cba70a346b1f71bfb3bd399556b (patch)
treebfec39fe84b547dac991f0d38cd69e9c64c688a6 /src/org/traccar/geocoder/JsonGeocoder.java
parent404044342a9fc9891366fa4378b4b1330cbca6c3 (diff)
downloadtrackermap-server-96d89e2e352f5cba70a346b1f71bfb3bd399556b.tar.gz
trackermap-server-96d89e2e352f5cba70a346b1f71bfb3bd399556b.tar.bz2
trackermap-server-96d89e2e352f5cba70a346b1f71bfb3bd399556b.zip
- Combine "getAddress" functions
- Change switches logic
Diffstat (limited to 'src/org/traccar/geocoder/JsonGeocoder.java')
-rw-r--r--src/org/traccar/geocoder/JsonGeocoder.java95
1 files changed, 44 insertions, 51 deletions
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;
}