Skip to content

Commit ab7d289

Browse files
committed
Change nearest point calculations to Leaflet
1 parent 62a3955 commit ab7d289

15 files changed

+28953
-10
lines changed

css/leaflet.css

+640
Large diffs are not rendered by default.

css/styles.css

+3
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
}
44
.menu li {
55
display: inline;
6+
}
7+
#mapid {
8+
height: 180px;
69
}

index.html

+16-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html>
33
<head>
44
<link rel="stylesheet" href="css/styles.css">
5+
<link rel="stylesheet" href="/css/leaflet.css" />
56

67
<!-- Matomo -->
78
<script type="text/javascript">
@@ -20,7 +21,8 @@
2021
<!-- End Matomo Code -->
2122

2223
</head>
23-
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script>
24+
<script src="js/leaflet/leaflet.js"></script>
25+
<script src="js/leaflet/leaflet-knn.js"></script>
2426
<script src="js/LoadData.js"></script>
2527
<script src="js/moment.js"></script>
2628
<script>
@@ -50,36 +52,39 @@
5052
}
5153

5254
function showPosition(position) {
53-
5455
document.getElementById("status").innerHTML = "Running calculations";
5556

56-
var currentLocation = turf.point([position.coords.longitude, position.coords.latitude]);
57-
57+
var gj = L.geoJson(data);
58+
5859
// 0.3 miles from AICW 6
59-
//var currentLocation = turf.point([-76.2932567,36.7960833]);
60+
//var currentLocation = L.latLng(36.7960833, -76.2932567);
61+
var currentLocation = L.latLng(position.coords.latitude, position.coords.longitude);
6062

6163
document.getElementById("status").innerHTML = "Running calculations...";
62-
var nearest = turf.nearestPoint(currentLocation, data);
64+
var nearest = leafletKnn(gj).nearest(currentLocation, 1);
6365

6466
document.getElementById("status").innerHTML = "Setting position values";
6567
document.getElementById("latitude").innerHTML = position.coords.latitude.toFixed(5);
6668
document.getElementById("longitude").innerHTML = position.coords.longitude.toFixed(5);
6769

6870
document.getElementById("status").innerHTML = "Setting distance value";
69-
//document.getElementById("distance").innerHTML = (nearest.properties.distanceToPoint / 1.852).toFixed(2) + " nautical miles";
71+
var point2 = L.latLng(nearest[0].lat, nearest[0].lon)
72+
var distance = currentLocation.distanceTo(point2);
73+
74+
document.getElementById("distance").innerHTML = (distance / 1852).toFixed(2) + " nm";
7075

7176
document.getElementById("status").innerHTML = "Setting accuracy value";
72-
document.getElementById("accuracy").innerHTML = position.coords.accuracy.toFixed(0) + " meters";
77+
document.getElementById("accuracy").innerHTML = (position.coords.accuracy / 1852).toFixed(3) + " nm";
7378

7479

7580
document.getElementById("status").innerHTML = "Setting nearest point";
7681
if (position.coords.accuracy > 1000) {
7782
document.getElementById("status").innerHTML = "Setting nearest point (inaccurate)";
78-
document.getElementById("nearest").innerHTML = nearest.properties.name + " maybe";
83+
document.getElementById("nearest").innerHTML = nearest[0].layer.feature.properties.name + " maybe";
7984
}
8085
else {
8186
document.getElementById("status").innerHTML = "Setting nearest point (accurate)";
82-
document.getElementById("nearest").innerHTML = nearest.properties.name;
87+
document.getElementById("nearest").innerHTML = nearest[0].layer.feature.properties.name;
8388
}
8489
document.getElementById("status").innerHTML = "Getting current time";
8590
var now = new moment();
@@ -143,5 +148,6 @@ <h1 id="nearest"></h1>
143148
<td id="distance"></td>
144149
</tr>
145150
</table>
151+
<div id="mapid"></div>
146152
</body>
147153
</html>

js/leaflet/images/layers-2x.png

1.23 KB
Loading

js/leaflet/images/layers.png

696 Bytes
Loading

js/leaflet/images/marker-icon-2x.png

2.41 KB
Loading

js/leaflet/images/marker-icon.png

1.43 KB
Loading

js/leaflet/images/marker-shadow.png

618 Bytes
Loading

0 commit comments

Comments
 (0)