Skip to content

Commit

Permalink
Refactored code to add new location and util files.
Browse files Browse the repository at this point in the history
  • Loading branch information
shivanibeehyv committed Jan 17, 2019
1 parent 98af3a4 commit 08dc682
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 32 deletions.
7 changes: 4 additions & 3 deletions WebContent/index.jsp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="map.Lookup" %>
<%@ page import="map.Location" %>
<html>
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
Expand Down Expand Up @@ -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(); %>
<script>gotoLoc(<%=lat%>, <%=lon%>);</script>
<%}
}%>
Expand Down
29 changes: 29 additions & 0 deletions src/map/Location.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
32 changes: 3 additions & 29 deletions src/map/Lookup.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
38 changes: 38 additions & 0 deletions src/map/Util.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 08dc682

Please sign in to comment.