Skip to content

Commit

Permalink
merged changes
Browse files Browse the repository at this point in the history
  • Loading branch information
shivanibeehyv committed Jan 17, 2019
2 parents 4cf7edb + 2c44c31 commit 98af3a4
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 19 deletions.
10 changes: 10 additions & 0 deletions WebContent/CSS/Main.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ pre {
width: 80%;
}

.place {
float: left;
padding-right: 5px;
margin-right: 5px;
}

#place-input {
width: 200px;
}

.map {
height: 500px;
width: 80%;
Expand Down
29 changes: 14 additions & 15 deletions WebContent/JS/Main.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@

window.onload = pageLoad

function pageLoad() {
initMap()
}
initMap()

function initMap() {
var map = new ol.Map({
window.view = new ol.View({
center: ol.proj.fromLonLat([-121.751392674913, 38.52247515]),
zoom: 13
})
window.map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.fromLonLat([37.41, 8.82]),
zoom: 4
})
});
navigator.geolocation.getCurrentPosition(function(pos) {
const coords = ol.proj.fromLonLat([pos.coords.longitude, pos.coords.latitude]);
map.getView().animate({center: coords, zoom: 13});
view: window.view
});
}
}

function gotoLoc(lat, lon) {
const coords = ol.proj.fromLonLat([lon, lat]);
window.view.animate({center: coords, zoom: 13});
// window.view.fit(ol.proj.transformExtent([minLon, minLat, maxLon, maxLat], 'EPSG:4326', 'EPSG:3857'))
}
28 changes: 24 additions & 4 deletions WebContent/index.jsp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="map.Lookup" %>
<html>
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
Expand All @@ -7,6 +8,7 @@
<title>StudyUp!</title>
</head>
<body>
<!-- Navbar CODE -->
<nav class="navbar navbar-expand-lg navbar-light bg-light static-top">
<div class="container">
<a class="navbar-brand" href="#">
Expand Down Expand Up @@ -36,11 +38,18 @@
<div class="container">
<section id="Home">
<h1 class="mt-4">Meet up to up your grades!</h1>
<p>Alas, this page does nearly nothing :( Let's make it beautiful this quarter.
For now, why don't you add a button below to take us to your location?</p>
<p>Alas, this page does nearly nothing :( Let's make it beautiful this quarter.</p>
<div id="map" class="map"></div>
<form action="index.jsp" method="GET">
<div class="form-group">
<input name="place" id="place-input" class="place form-control"
placeholder="Search for a place"
value="<%=request.getParameter("place") == null ? "" : request.getParameter("place")%>">
<button type="submit" id="place-submit" class="place btn btn-dark">Submit</button>
</div>
</form>
</section>

<br/>
<section id="About">
<h1 class="mt-4">About</h1>
<p>This simple web-app will be our basis for teaching various programming tools over the course of this quarter.
Expand All @@ -55,11 +64,22 @@
</section>
</div>

<!-- YOUR CODE HERE -->
<!-- JS CODE -->
<script type="text/javascript" src="webjars/jquery/3.3.1-1/jquery.min.js"></script>
<script type="text/javascript" src="webjars/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<script type="text/javascript" src="webjars/bootstrap/4.2.1/js/bootstrap.bundle.js"></script>
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<script src="JS/Main.js"></script>

<%
String place = request.getParameter("place");
if (place != null && !place.isEmpty()) {
String loc = Lookup.lookupPlace(place);
if (loc != null) {
double lat = Double.parseDouble(loc.split("\t")[0]);
double lon = Double.parseDouble(loc.split("\t")[1]); %>
<script>gotoLoc(<%=lat%>, <%=lon%>);</script>
<%}
}%>
</body>
</html>
42 changes: 42 additions & 0 deletions src/map/Lookup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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();
}
if (results.isEmpty()) return null;

JSONObject best = results.getJSONObject(0);
double lat = best.getDouble("lat");
double lon = best.getDouble("lon");
return lat + "\t" + lon;
}
}

0 comments on commit 98af3a4

Please sign in to comment.