aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/geocode/GoogleReverseGeocoder.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-06-23 12:21:37 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-06-23 12:21:37 +1200
commitec7b47684c89264f4347f2601a83d602162cb817 (patch)
tree7c6fafb35abe6990f65e57a64af7c026d46a97e6 /src/org/traccar/geocode/GoogleReverseGeocoder.java
parent7dec68dabcb5690f7c4092a0d0df17d787c9176c (diff)
downloadtrackermap-server-ec7b47684c89264f4347f2601a83d602162cb817.tar.gz
trackermap-server-ec7b47684c89264f4347f2601a83d602162cb817.tar.bz2
trackermap-server-ec7b47684c89264f4347f2601a83d602162cb817.zip
Fix resource leaking in geocoders
Diffstat (limited to 'src/org/traccar/geocode/GoogleReverseGeocoder.java')
-rw-r--r--src/org/traccar/geocode/GoogleReverseGeocoder.java75
1 files changed, 34 insertions, 41 deletions
diff --git a/src/org/traccar/geocode/GoogleReverseGeocoder.java b/src/org/traccar/geocode/GoogleReverseGeocoder.java
index efefdb0f6..df8d99545 100644
--- a/src/org/traccar/geocode/GoogleReverseGeocoder.java
+++ b/src/org/traccar/geocode/GoogleReverseGeocoder.java
@@ -24,60 +24,53 @@ import javax.json.JsonObject;
import javax.json.JsonString;
import org.traccar.helper.Log;
-public class GoogleReverseGeocoder implements ReverseGeocoder {
+public class GoogleReverseGeocoder extends JsonReverseGeocoder {
- private final String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f";
+ public GoogleReverseGeocoder() {
+ super("http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f");
+ }
@Override
- public String getAddress(AddressFormat format, double latitude, double longitude) {
+ protected Address parseAddress(JsonObject json) {
+ JsonArray results = json.getJsonArray("results");
- try {
+ if (!results.isEmpty()) {
Address address = new Address();
- URLConnection conn = new URL(String.format(url, latitude, longitude)).openConnection();
-
- JsonObject json = Json.createReader(new InputStreamReader(conn.getInputStream())).readObject();
- JsonArray results = json.getJsonArray("results");
- if (!results.isEmpty()) {
+ JsonObject result = (JsonObject) results.get(0);
+ JsonArray components = result.getJsonArray("address_components");
- JsonObject result = (JsonObject) results.get(0);
- JsonArray components = result.getJsonArray("address_components");
+ for (JsonObject component : components.getValuesAs(JsonObject.class)) {
- for (JsonObject component : components.getValuesAs(JsonObject.class)) {
+ String value = component.getString("short_name");
- String value = component.getString("short_name");
-
- for (JsonString type : component.getJsonArray("types").getValuesAs(JsonString.class)) {
- if (type.getString().equals("street_number")) {
- address.setHouse(value);
- break;
- } else if (type.getString().equals("route")) {
- address.setStreet(value);
- break;
- } else if (type.getString().equals("locality")) {
- address.setSettlement(value);
- break;
- } else if (type.getString().equals("administrative_area_level_2")) {
- address.setDistrict(value);
- break;
- } else if (type.getString().equals("administrative_area_level_1")) {
- address.setState(value);
- break;
- } else if (type.getString().equals("country")) {
- address.setCountry(value);
- break;
- } else if (type.getString().equals("postal_code")) {
- address.setPostcode(value);
- break;
- }
+ for (JsonString type : component.getJsonArray("types").getValuesAs(JsonString.class)) {
+ if (type.getString().equals("street_number")) {
+ address.setHouse(value);
+ break;
+ } else if (type.getString().equals("route")) {
+ address.setStreet(value);
+ break;
+ } else if (type.getString().equals("locality")) {
+ address.setSettlement(value);
+ break;
+ } else if (type.getString().equals("administrative_area_level_2")) {
+ address.setDistrict(value);
+ break;
+ } else if (type.getString().equals("administrative_area_level_1")) {
+ address.setState(value);
+ break;
+ } else if (type.getString().equals("country")) {
+ address.setCountry(value);
+ break;
+ } else if (type.getString().equals("postal_code")) {
+ address.setPostcode(value);
+ break;
}
}
-
- return format.format(address);
}
- } catch(Exception error) {
- Log.warning(error);
+ return address;
}
return null;