diff --git a/WebContent/index.jsp b/WebContent/index.jsp index 3d088976..03ebf310 100644 --- a/WebContent/index.jsp +++ b/WebContent/index.jsp @@ -1,5 +1,6 @@ <%@ page contentType="text/html;charset=UTF-8" language="java"%> <%@ page import="map.Lookup" %> +<%@ page import="map.Location" %>
@@ -74,10 +75,10 @@ <% String place = request.getParameter("place"); if (place != null && !place.isEmpty()) { - String loc = Lookup.lookupPlace(place); + Location loc = Lookup.lookupPlace(place); if (loc != null) { - double lat = Double.parseDouble(loc.split("\t")[0]); - double lon = Double.parseDouble(loc.split("\t")[1]); %> + double lat = loc.getLat(); + double lon = loc.getLon(); %> <%} }%> diff --git a/src/map/Location.java b/src/map/Location.java new file mode 100644 index 00000000..3e7f5bc5 --- /dev/null +++ b/src/map/Location.java @@ -0,0 +1,29 @@ +package map; + +public class Location { + + private double lat; + private double lon; + + public Location(double lat, double lon) { + this.setLat(lat); + this.setLon(lon); + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } + + public double getLon() { + return lon; + } + + public void setLon(double lon) { + this.lon = lon; + } + +} diff --git a/src/map/Lookup.java b/src/map/Lookup.java index 8e08f48f..2c171d9d 100644 --- a/src/map/Lookup.java +++ b/src/map/Lookup.java @@ -1,42 +1,16 @@ package map; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLEncoder; - import org.json.JSONArray; import org.json.JSONObject; public class Lookup { - public static String lookupPlace(String query) { - JSONArray results = new JSONArray(); - try { - String urlString = "https://nominatim.openstreetmap.org/search?q=" + URLEncoder.encode(query, "UTF-8") + "&format=json"; - URL url = new URL(urlString); - try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) { - StringBuilder sb = new StringBuilder(); - int cp; - while ((cp = in.read()) != -1) { - sb.append((char) cp); - } - in.close(); - String content = sb.toString(); - results = new JSONArray(content); - } catch (IOException e) { - e.printStackTrace(); - } - } catch (UnsupportedEncodingException | MalformedURLException e) { - e.printStackTrace(); - } + public static Location lookupPlace(String query) { + JSONArray results = Util.queryURL(query); if (results.isEmpty()) return null; JSONObject best = results.getJSONObject(0); double lat = best.getDouble("lat"); double lon = best.getDouble("lon"); - return lat + "\t" + lon; + return new Location(lat, lon); } } diff --git a/src/map/Util.java b/src/map/Util.java new file mode 100644 index 00000000..b3098aca --- /dev/null +++ b/src/map/Util.java @@ -0,0 +1,38 @@ +package map; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLEncoder; + +import org.json.JSONArray; + +public class Util { + + static JSONArray queryURL(String query) { + JSONArray results = new JSONArray(); + try { + String urlString = "https://nominatim.openstreetmap.org/search?q=" + URLEncoder.encode(query, "UTF-8") + "&format=json"; + URL url = new URL(urlString); + try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) { + StringBuilder sb = new StringBuilder(); + int cp; + while ((cp = in.read()) != -1) { + sb.append((char) cp); + } + in.close(); + String content = sb.toString(); + results = new JSONArray(content); + } catch (IOException e) { + e.printStackTrace(); + } + } catch (UnsupportedEncodingException | MalformedURLException e) { + e.printStackTrace(); + } + return results; + } + +}