aboutsummaryrefslogtreecommitdiff
path: root/src/org
diff options
context:
space:
mode:
Diffstat (limited to 'src/org')
-rw-r--r--src/org/traccar/geocode/GoogleReverseGeocoder.java68
1 files changed, 37 insertions, 31 deletions
diff --git a/src/org/traccar/geocode/GoogleReverseGeocoder.java b/src/org/traccar/geocode/GoogleReverseGeocoder.java
index dae9915d8..efefdb0f6 100644
--- a/src/org/traccar/geocode/GoogleReverseGeocoder.java
+++ b/src/org/traccar/geocode/GoogleReverseGeocoder.java
@@ -18,11 +18,12 @@ package org.traccar.geocode;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
-
+import javax.json.Json;
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.json.JsonString;
import org.traccar.helper.Log;
-import javax.json.*;
-
public class GoogleReverseGeocoder implements ReverseGeocoder {
private final String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f";
@@ -35,40 +36,45 @@ public class GoogleReverseGeocoder implements ReverseGeocoder {
URLConnection conn = new URL(String.format(url, latitude, longitude)).openConnection();
JsonObject json = Json.createReader(new InputStreamReader(conn.getInputStream())).readObject();
- JsonObject result = (JsonObject) json.getJsonArray("results").get(0);
- JsonArray components = result.getJsonArray("address_components");
+ JsonArray results = json.getJsonArray("results");
+
+ if (!results.isEmpty()) {
- for (JsonObject component : components.getValuesAs(JsonObject.class)) {
+ JsonObject result = (JsonObject) results.get(0);
+ JsonArray components = result.getJsonArray("address_components");
- String value = component.getString("short_name");
+ for (JsonObject component : components.getValuesAs(JsonObject.class)) {
- 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;
+ 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;
+ }
}
}
- }
- return format.format(address);
+ return format.format(address);
+ }
} catch(Exception error) {
Log.warning(error);