|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.InputStreamReader; |
| 3 | +import java.net.HttpURLConnection; |
| 4 | +import java.net.URL; |
| 5 | +import org.json.JSONArray; |
| 6 | +import org.json.JSONObject; |
| 7 | + |
| 8 | +public class GeocodeFarm { |
| 9 | + |
| 10 | + private String apiKey; |
| 11 | + |
| 12 | + public GeocodeFarm(String apiKey) { |
| 13 | + this.apiKey = apiKey; |
| 14 | + } |
| 15 | + |
| 16 | + // Forward Geocoding |
| 17 | + public GeocodeResponse forward(String address) { |
| 18 | + try { |
| 19 | + String url = "https://api.geocode.farm/forward/?key=" + this.apiKey + "&addr=" + address; |
| 20 | + String response = makeRequest(url); |
| 21 | + JSONObject jsonResponse = new JSONObject(response); |
| 22 | + return parseResponse(jsonResponse, "forward"); |
| 23 | + } catch (Exception e) { |
| 24 | + e.printStackTrace(); |
| 25 | + return new GeocodeResponse(false, "Request failed or timed out", null, null, null, null, null); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + // Reverse Geocoding |
| 30 | + public GeocodeResponse reverse(double lat, double lon) { |
| 31 | + try { |
| 32 | + String url = "https://api.geocode.farm/reverse/?key=" + this.apiKey + "&lat=" + lat + "&lon=" + lon; |
| 33 | + String response = makeRequest(url); |
| 34 | + JSONObject jsonResponse = new JSONObject(response); |
| 35 | + return parseResponse(jsonResponse, "reverse"); |
| 36 | + } catch (Exception e) { |
| 37 | + e.printStackTrace(); |
| 38 | + return new GeocodeResponse(false, "Request failed or timed out", null, null, null, null, null); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Make the HTTP request |
| 43 | + private String makeRequest(String urlString) throws Exception { |
| 44 | + URL url = new URL(urlString); |
| 45 | + HttpURLConnection con = (HttpURLConnection) url.openConnection(); |
| 46 | + con.setRequestMethod("GET"); |
| 47 | + con.setRequestProperty("User-Agent", "GeocodeFarmSDK/1.0"); |
| 48 | + |
| 49 | + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); |
| 50 | + String inputLine; |
| 51 | + StringBuffer response = new StringBuffer(); |
| 52 | + |
| 53 | + while ((inputLine = in.readLine()) != null) { |
| 54 | + response.append(inputLine); |
| 55 | + } |
| 56 | + in.close(); |
| 57 | + return response.toString(); |
| 58 | + } |
| 59 | + |
| 60 | + // Parse the response and break it down |
| 61 | + private GeocodeResponse parseResponse(JSONObject data, String type) { |
| 62 | + if (data == null || !data.has("RESULTS")) { |
| 63 | + return new GeocodeResponse(false, "Invalid response from server", null, null, null, null, null); |
| 64 | + } |
| 65 | + |
| 66 | + JSONObject status = data.getJSONObject("STATUS"); |
| 67 | + String statusCode = status.getString("status"); |
| 68 | + if (!statusCode.equals("SUCCESS")) { |
| 69 | + return new GeocodeResponse(false, "API returned failure: " + statusCode, null, null, null, null, null); |
| 70 | + } |
| 71 | + |
| 72 | + JSONArray resultArray = data.getJSONObject("RESULTS").getJSONArray("result"); |
| 73 | + if (resultArray.length() == 0) { |
| 74 | + return new GeocodeResponse(false, "No results found", null, null, null, null, null); |
| 75 | + } |
| 76 | + |
| 77 | + JSONObject resultData = resultArray.getJSONObject(0); |
| 78 | + |
| 79 | + if (type.equals("reverse")) { |
| 80 | + return parseReverse(resultData); |
| 81 | + } else { |
| 82 | + return parseForward(resultData); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Parse reverse geocoding result |
| 87 | + private GeocodeResponse parseReverse(JSONObject resultData) { |
| 88 | + return new GeocodeResponse( |
| 89 | + true, |
| 90 | + null, |
| 91 | + resultData.optString("house_number", null), |
| 92 | + resultData.optString("street_name", null), |
| 93 | + resultData.optString("locality", null), |
| 94 | + resultData.optString("formatted_address", null), |
| 95 | + resultData.optDouble("accuracy", 0.0) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + // Parse forward geocoding result |
| 100 | + private GeocodeResponse parseForward(JSONObject resultData) { |
| 101 | + JSONObject coordinates = resultData.optJSONObject("coordinates"); |
| 102 | + return new GeocodeResponse( |
| 103 | + true, |
| 104 | + null, |
| 105 | + resultData.optString("address", null), |
| 106 | + resultData.optString("locality", null), |
| 107 | + resultData.optString("formatted_address", null), |
| 108 | + coordinates != null ? coordinates.optString("full_address", null) : null, |
| 109 | + resultData.optDouble("accuracy", 0.0) |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + // GeocodeResponse class to structure the response |
| 114 | + public static class GeocodeResponse { |
| 115 | + private boolean success; |
| 116 | + private String error; |
| 117 | + private String houseNumber; |
| 118 | + private String streetName; |
| 119 | + private String locality; |
| 120 | + private String fullAddress; |
| 121 | + private double accuracy; |
| 122 | + |
| 123 | + public GeocodeResponse(boolean success, String error, String houseNumber, String streetName, String locality, String fullAddress, double accuracy) { |
| 124 | + this.success = success; |
| 125 | + this.error = error; |
| 126 | + this.houseNumber = houseNumber; |
| 127 | + this.streetName = streetName; |
| 128 | + this.locality = locality; |
| 129 | + this.fullAddress = fullAddress; |
| 130 | + this.accuracy = accuracy; |
| 131 | + } |
| 132 | + |
| 133 | + // Getters for the response data |
| 134 | + public boolean isSuccess() { |
| 135 | + return success; |
| 136 | + } |
| 137 | + |
| 138 | + public String getError() { |
| 139 | + return error; |
| 140 | + } |
| 141 | + |
| 142 | + public String getHouseNumber() { |
| 143 | + return houseNumber; |
| 144 | + } |
| 145 | + |
| 146 | + public String getStreetName() { |
| 147 | + return streetName; |
| 148 | + } |
| 149 | + |
| 150 | + public String getLocality() { |
| 151 | + return locality; |
| 152 | + } |
| 153 | + |
| 154 | + public String getFullAddress() { |
| 155 | + return fullAddress; |
| 156 | + } |
| 157 | + |
| 158 | + public double getAccuracy() { |
| 159 | + return accuracy; |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments