diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-23 12:21:37 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-23 12:21:37 +1200 |
commit | ec7b47684c89264f4347f2601a83d602162cb817 (patch) | |
tree | 7c6fafb35abe6990f65e57a64af7c026d46a97e6 /src/org/traccar/geocode/NominatimReverseGeocoder.java | |
parent | 7dec68dabcb5690f7c4092a0d0df17d787c9176c (diff) | |
download | trackermap-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/NominatimReverseGeocoder.java')
-rw-r--r-- | src/org/traccar/geocode/NominatimReverseGeocoder.java | 70 |
1 files changed, 29 insertions, 41 deletions
diff --git a/src/org/traccar/geocode/NominatimReverseGeocoder.java b/src/org/traccar/geocode/NominatimReverseGeocoder.java index 911e0fa71..6777f5a82 100644 --- a/src/org/traccar/geocode/NominatimReverseGeocoder.java +++ b/src/org/traccar/geocode/NominatimReverseGeocoder.java @@ -27,59 +27,47 @@ import javax.xml.parsers.DocumentBuilderFactory; import org.traccar.helper.Log; import org.w3c.dom.Document; -public class NominatimReverseGeocoder implements ReverseGeocoder { - - private final String url; +public class NominatimReverseGeocoder extends JsonReverseGeocoder { public NominatimReverseGeocoder() { this("http://nominatim.openstreetmap.org/reverse"); } public NominatimReverseGeocoder(String url) { - this.url = url + "?format=json&lat=%f&lon=%f&zoom=18&addressdetails=1"; + super(url + "?format=json&lat=%f&lon=%f&zoom=18&addressdetails=1"); } @Override - public String getAddress(AddressFormat format, double latitude, double longitude) { - - try { - Address address = new Address(); - URLConnection conn = new URL(String.format(url, latitude, longitude)).openConnection(); - - JsonObject json = Json.createReader(new InputStreamReader(conn.getInputStream())).readObject().getJsonObject("address"); - - if (json.containsKey("house_number")) { - address.setHouse(json.getString("house_number")); - } - if (json.containsKey("road")) { - address.setStreet(json.getString("road")); - } - if (json.containsKey("village")) { - address.setSettlement(json.getString("village")); - } - if (json.containsKey("city")) { - address.setSettlement(json.getString("city")); - } - if (json.containsKey("state_district")) { - address.setDistrict(json.getString("state_district")); - } - if (json.containsKey("state")) { - address.setState(json.getString("state")); - } - if (json.containsKey("country_code")) { - address.setCountry(json.getString("country_code").toUpperCase()); - } - if (json.containsKey("postcode")) { - address.setPostcode(json.getString("postcode")); - } + protected Address parseAddress(JsonObject json) { + Address address = new Address(); + JsonObject result = json.getJsonObject("address"); - return format.format(address); - - } catch(Exception error) { - Log.warning(error); + if (result.containsKey("house_number")) { + address.setHouse(result.getString("house_number")); + } + if (result.containsKey("road")) { + address.setStreet(result.getString("road")); + } + if (result.containsKey("village")) { + address.setSettlement(result.getString("village")); + } + if (result.containsKey("city")) { + address.setSettlement(result.getString("city")); + } + if (result.containsKey("state_district")) { + address.setDistrict(result.getString("state_district")); + } + if (result.containsKey("state")) { + address.setState(result.getString("state")); + } + if (result.containsKey("country_code")) { + address.setCountry(result.getString("country_code").toUpperCase()); + } + if (result.containsKey("postcode")) { + address.setPostcode(result.getString("postcode")); } - return null; + return address; } } |