diff options
-rw-r--r-- | src/org/traccar/Context.java | 20 | ||||
-rw-r--r-- | src/org/traccar/geocoder/GeocodeFarmGeocoder.java | 13 | ||||
-rw-r--r-- | src/org/traccar/geocoder/GoogleGeocoder.java | 19 | ||||
-rw-r--r-- | src/org/traccar/geocoder/NominatimGeocoder.java | 16 | ||||
-rw-r--r-- | test/org/traccar/geocoder/GeocoderTest.java | 12 |
5 files changed, 38 insertions, 42 deletions
diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index cd6820778..c9c2e633e 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -208,15 +208,12 @@ public final class Context { String type = config.getString("geocoder.type", "google"); String url = config.getString("geocoder.url"); String key = config.getString("geocoder.key"); + String language = config.getString("geocoder.language"); int cacheSize = config.getInteger("geocoder.cacheSize"); switch (type) { case "nominatim": - if (key != null) { - geocoder = new NominatimGeocoder(url, key, cacheSize); - } else { - geocoder = new NominatimGeocoder(url, cacheSize); - } + geocoder = new NominatimGeocoder(url, key, cacheSize); break; case "gisgraphy": geocoder = new GisgraphyGeocoder(url, cacheSize); @@ -234,17 +231,10 @@ public final class Context { geocoder = new FactualGeocoder(url, key, cacheSize); break; case "geocodefarm": - if (key != null) { - geocoder = new GeocodeFarmGeocoder(key, cacheSize); - } else { - geocoder = new GeocodeFarmGeocoder(cacheSize); - } + geocoder = new GeocodeFarmGeocoder(key, cacheSize); + break; default: - if (key != null) { - geocoder = new GoogleGeocoder(key, cacheSize); - } else { - geocoder = new GoogleGeocoder(cacheSize); - } + geocoder = new GoogleGeocoder(key, language, cacheSize); break; } } diff --git a/src/org/traccar/geocoder/GeocodeFarmGeocoder.java b/src/org/traccar/geocoder/GeocodeFarmGeocoder.java index 585095606..db23aab79 100644 --- a/src/org/traccar/geocoder/GeocodeFarmGeocoder.java +++ b/src/org/traccar/geocoder/GeocodeFarmGeocoder.java @@ -19,14 +19,15 @@ import javax.json.JsonObject; public class GeocodeFarmGeocoder extends JsonGeocoder { - private static final String URL = "https://www.geocode.farm/v3/json/reverse/"; - - public GeocodeFarmGeocoder(int cacheSize) { - super(URL + "?lat=%f&lon=%f&country=us&lang=en&count=1", cacheSize); + private static String formatUrl(String key) { + String url = "https://www.geocode.farm/v3/json/reverse/"; + if (key != null) { + url += "&key=" + key; + } + return url; } - public GeocodeFarmGeocoder(String key, int cacheSize) { - super(URL + "?lat=%f&lon=%f&country=us&lang=en&count=1&key=" + key, cacheSize); + super(formatUrl(key), cacheSize); } @Override diff --git a/src/org/traccar/geocoder/GoogleGeocoder.java b/src/org/traccar/geocoder/GoogleGeocoder.java index 0506e701a..b38870c8f 100644 --- a/src/org/traccar/geocoder/GoogleGeocoder.java +++ b/src/org/traccar/geocoder/GoogleGeocoder.java @@ -21,16 +21,19 @@ import javax.json.JsonString; public class GoogleGeocoder extends JsonGeocoder { - public GoogleGeocoder() { - this(0); - } - - public GoogleGeocoder(int cacheSize) { - super("http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f", cacheSize); + private static String formatUrl(String key, String language) { + String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f"; + if (key != null) { + url += "&key=" + key; + } + if (language != null) { + url += "&language=" + language; + } + return url; } - public GoogleGeocoder(String key, int cacheSize) { - super("https://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&key=" + key, cacheSize); + public GoogleGeocoder(String key, String language, int cacheSize) { + super(formatUrl(key, language), cacheSize); } @Override diff --git a/src/org/traccar/geocoder/NominatimGeocoder.java b/src/org/traccar/geocoder/NominatimGeocoder.java index b0ee39c6a..c10a1414e 100644 --- a/src/org/traccar/geocoder/NominatimGeocoder.java +++ b/src/org/traccar/geocoder/NominatimGeocoder.java @@ -19,16 +19,18 @@ import javax.json.JsonObject; public class NominatimGeocoder extends JsonGeocoder { - public NominatimGeocoder() { - this("http://nominatim.openstreetmap.org/reverse", 0); - } - - public NominatimGeocoder(String url, int cacheSize) { - super(url + "?format=json&lat=%f&lon=%f&zoom=18&addressdetails=1", cacheSize); + private static String formatUrl(String url, String key) { + if (url == null) { + url = "http://nominatim.openstreetmap.org/reverse"; + } + if (key != null) { + url += "&key=" + key; + } + return url; } public NominatimGeocoder(String url, String key, int cacheSize) { - super(url + "?format=json&lat=%f&lon=%f&zoom=18&addressdetails=1&key=" + key, cacheSize); + super(formatUrl(url, key), cacheSize); } @Override diff --git a/test/org/traccar/geocoder/GeocoderTest.java b/test/org/traccar/geocoder/GeocoderTest.java index 012f8bacd..60e415846 100644 --- a/test/org/traccar/geocoder/GeocoderTest.java +++ b/test/org/traccar/geocoder/GeocoderTest.java @@ -10,7 +10,7 @@ public class GeocoderTest { @Test public void test() throws InterruptedException { if (enable) { - testGeocodeFarm(); + testGoogle(); } } @@ -31,9 +31,9 @@ public class GeocoderTest { } public void testGoogle() throws InterruptedException { - Geocoder geocoder = new GoogleGeocoder(); + Geocoder geocoder = new GoogleGeocoder(null, null, 0); - geocoder.getAddress(new AddressFormat(), 37.4217550, -122.0846330, new Geocoder.ReverseGeocoderCallback() { + geocoder.getAddress(new AddressFormat(), 31.776797, 35.211489, new Geocoder.ReverseGeocoderCallback() { @Override public void onSuccess(String address) { setAddress(address); @@ -43,11 +43,11 @@ public class GeocoderTest { public void onFailure(Throwable e) { } }); - Assert.assertEquals("1600 Amphitheatre Pkwy, Mountain View, CA, US", waitAddress()); + Assert.assertEquals("1 Ibn Shaprut St, Jerusalem, Jerusalem District, IL", waitAddress()); } public void testNominatim() throws InterruptedException { - Geocoder geocoder = new NominatimGeocoder(); + Geocoder geocoder = new NominatimGeocoder(null, null, 0); geocoder.getAddress(new AddressFormat(), 40.7337807, -73.9974401, new Geocoder.ReverseGeocoderCallback() { @Override @@ -96,7 +96,7 @@ public class GeocoderTest { } public void testGeocodeFarm() throws InterruptedException { - Geocoder geocoder = new GeocodeFarmGeocoder(0); + Geocoder geocoder = new GeocodeFarmGeocoder(null, 0); geocoder.getAddress(new AddressFormat(), 34.116302, -118.051519, new Geocoder.ReverseGeocoderCallback() { @Override |