This repository was archived by the owner on Nov 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
231 lines (192 loc) · 11.7 KB
/
Copy pathindex.html
File metadata and controls
231 lines (192 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<title>MyNavigate</title>
<link rel="icon" href="img/icon.png">
<!-- Custom styles for this template -->
<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/crimeMap.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Raleway:wght@600&display=swap" rel="stylesheet">
<!-- Import map information -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js" integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew==" crossorigin=""></script>
<!--Applies Google autofill on start point and destination input label (both created in <div class = "location">)-->
<!--Places a marker and makes the user's map "fly" to the entered start point and destinaton-->
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCCoPdq7VqBX2WU0ZRWjmLqtTVTe00qN3o&libraries=places"></script>
<script>
function initialize() {
var input = document.getElementById('searchTextStart');
var autocomplete = new google.maps.places.Autocomplete(input);
//lat1 and lng1 default location set to be campus center: Illini Union = 40.1092101, -88.2294165
var lat1 = 40.1092101;
var lng1 = -88.2294165;
var lat2, lng2;
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place1 = autocomplete.getPlace();
var name1 = place1.name;
//Gathers and stores the user-inputed starting latitude and longitude
var lat1 = place1.geometry.location.lat();
document.getElementById("place1Lat").value = lat1;
var lng1 = place1.geometry.location.lng();
document.getElementById("place1Lng").value = lng1;
var start_location = [lat1, lng1];
map.flyTo(start_location, 15);
L.marker(start_location).addTo(map);
});
var input1 = document.getElementById('searchTextDestination');
var autocomplete1 = new google.maps.places.Autocomplete(input1);
google.maps.event.addListener(autocomplete1, 'place_changed', function() {
var place2 = autocomplete1.getPlace();
var name2 = place2.name;
//Gathers and stores the user-inputed destination latitude and longitude
var lat2 = place2.geometry.location.lat();
document.getElementById("place2Lat").value = lat2;
var lng2 = place2.geometry.location.lng();
document.getElementById("place2Lng").value = lng2;
// If the user enters the same input for the start point and destination, throw an error
if (lat1 = lat2 && lng1 == lng2) {
map.removeLayer(start_marker);
throw new Error(
`ERROR! Chosen start location and end location are the same!`
);
}
var end_location = [lat2, lng2];
map.flyTo(end_location, 15);
L.marker(end_location).addTo(map);
var bounds = L.latLngBounds([start_marker, destination_marker]);
map.fitBounds(bounds)
});
/* button.addEventListener("click", function(){
lat1 = 40.1092101;
lng1 = -88.2294165;
map.flyTo([lat1, lng1], 15);
}); */
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<!-- Import leaflet-locatecontrol information -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet.locatecontrol/dist/L.Control.Locate.min.css" />
<script src="https://cdn.jsdelivr.net/npm/leaflet.locatecontrol/dist/L.Control.Locate.min.js" charset="utf-8"></script>
</head>
<body id="page-top">
<h1>
<!--Adds website logo to the header-->
<div class="logo">
<img src="img/logo.png" alt="logo" />
</div>
<!--Adds brief website purpose to the header-->
<div class="description">
<p>Our crime map gives you the safest route for your journey.</p>
<p>Just input your start and end location and let our algorithm take care of the rest :)</p>
</div>
<!--Form that takes the user to enter the start point-->
<!--When the button is clicked, the createPath() method is called-->
<div class="location">
<div class="start-location">
<form id ="myform" class="form-inline my-2 my-lg-0">
<label id="destination-message">Select start point:</label>
<input id="searchTextStart" class="form-control mr-sm-2" type="text" placeholder="Search for start point" autocomplete="on" runat="server">
<input type="hidden" id="place1Lat" name="place1Lat" />
<input type="hidden" id="place1Lng" name="place1Lng" />
<button class="button btn btn-light my-2 my-sm-0" type="reset">Reset</button>
</form>
</div>
<div class="end-location">
<form id="myform1" class="form-inline my-2 my-lg-0">
<label id="destination-message">Select destination:</label>
<input id="searchTextDestination" class="form-control mr-sm-2" type="text" placeholder="Search for destination" autocomplete="on" runat="server">
<input type="hidden" id="place2Lat" name="place2Lat" />
<input type="hidden" id="place2Lng" name="place2Lng" />
<button class="button btn btn-light my-2 my-sm-0" type="reset">Reset</button>
</form>
</div>
<button class="center button btn btn-light my-2 my-sm-0" type="button" onclick="createPath()" >Search</button>
</div>
</h1>
<!--The div element for the map -->
<div id="mapid"></div>
<script>
// Set up map using Leaflet
var map = L.map('mapid').setView([
40.1092101, -88.2294165
], 13);
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1,
accessToken: 'pk.eyJ1Ijoid2VsYnltIiwiYSI6ImNrOHhmZ2cyZzBqanEzbXJ6b3BpaHhicHIifQ.5osNmEEJ_7PkwM5ADpkayg'
}).addTo(map);
var lc = L.control.locate({
setView: 'always'
}).addTo(map);
lc.start();
var popup = L.popup();
function onMapClick(e) {
popup.setLatLng(e.latlng).setContent("You clicked the map at " + e.latlng.toString()).openOn(map);
}
map.on('click', onMapClick);
// Illini Union coordinates = (40.1092101, -88.2294165)
var latstring = 40.1092;
var lonstring = -88.2294;
var distance = 7000; // Distance (miles)
var offset = 1000 * (1000*60*60); // Offset of time before current (hours)
var d = new Date();
var datetime_end = d.toISOString();
var datetwo = new Date(d - offset);
var datetime_ini = datetwo.toISOString();
// Link for the processing of parameters into a GET request
var url = "https://api.crimeometer.com/v1/incidents/raw-data?lat=" + latstring.toString() + "&lon=" + lonstring.toString() + "&distance="
+ distance.toString() + "mi&datetime_ini=" + datetime_ini + "&datetime_end=" + datetime_end;
var api_key = 'k3RAzKN1Ag14xTPlculT39RZb38LGgsG8n27ZycG';
var latitudes = [];
var longitudes = [];
function sendRequest(url) {
// Making an XML request
var request = new XMLHttpRequest();
request.open('GET', url);
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('x-api-key', api_key);
// Handles the response from the GET request
request.onreadystatechange = function() {
if (this.readyState === 4) {
// Parses results into a JSON object
// Stores the latitudes and longitudes of each crime into latitudes[] and longitudes[] respectively
var incidentsJson = JSON.parse(this.responseText);
for (var i = 0; i < incidentsJson.incidents.length; i++) {
latitudes.push(incidentsJson.incidents[i].incident_latitude);
longitudes.push(incidentsJson.incidents[i].incident_longitude);
}
//Plots each crime point on the map
for (var i = 0; i < latitudes.length; i++) {
L.circle([latitudes[i], longitudes[i]], 20, {
color: 'red',
}).addTo(map);
}
}
//Creates buffer polygons based on the coordinates of each crime point using a function found in main.js
window.crimePointsToPolygons(latitudes, longitudes);
};
request.send();
}
//Function for creating route with user-inputted start and destination that also avoids areas with recent crime
function createPath() {
// Get the points to create route for
var startLat = document.getElementById("place1Lat").value;
var startLng = document.getElementById("place1Lng").value;
var endLat = document.getElementById("place2Lat").value;
var endLng = document.getElementById("place2Lng").value;
// Create route for those points
window.createRoute(startLat, startLng, endLat, endLng);
}
sendRequest(url);
</script>
<!--Contains functions for creating buffer polygons and a route based on them.-->
<!--See main.js for implementation-->
<script type="text/javascript" src="bundle1.js"></script>
</body>
</html>