Skip to content

Commit 25ae05e

Browse files
find closest location
1 parent 1454e89 commit 25ae05e

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ venv
1616
*.pyc
1717
__pycache__/
1818
local_settings.py
19-
db.sqlite3
19+
db.sqlite3
20+
env/

database/bupehandler/static/js/mapInit.js

+47-1
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,56 @@ $(document).ready(function() {
5050
center: [-75.158924, 39.9629223],
5151
zoom: 11
5252
});
53+
54+
// Load Mapbox Geocoder
55+
const geocoder = new MapboxGeocoder({
56+
accessToken: mapboxgl.accessToken,
57+
mapboxgl: mapboxgl, // Set the mapbox-gl instance
58+
marker: true, // Use the geocoder's default marker style
59+
});
60+
61+
// Add Geocoder to map
62+
map.addControl(geocoder, 'top-right');
63+
64+
// Event listener for geocoder completion
65+
geocoder.on('result', ({ result }) => {
66+
const searchResult = result.geometry;
67+
const options = { units: 'miles' };
68+
69+
// Loop through sites and calculate distance to geocoder address
70+
for (const loc of data.loc) {
71+
loc.distance = turf.distance(
72+
searchResult,
73+
[loc.longitude, loc.latitude],
74+
options
75+
);
76+
}
77+
78+
// find site with min distance
79+
const closest = data.loc.sort((a, b) => {
80+
if (a.distance > b.distance) {
81+
return 1;
82+
}
83+
if (a.distance < b.distance) {
84+
return -1;
85+
}
86+
return 0; // a must be equal to b
87+
})[0];
88+
89+
// fit the map on the entered location and the closest site
90+
map.fitBounds([searchResult.coordinates, [closest.longitude, closest.latitude]], {padding: 200});
91+
92+
// Load popup of closest location
93+
link_object = window.location.origin + "/table/" + table_name + "/oid=" + closest.oid + "/";
94+
const popup = new mapboxgl.Popup()
95+
.setLngLat([closest.longitude, closest.latitude])
96+
.setHTML("<a href=" + link_object + ">" + JSON.stringify(closest.name1) + "</a>" )
97+
.addTo(map);
98+
});
99+
53100
document.querySelector("#sitecount").textContent = data['loc'].length
54101
var link_object;
55102
for (i = 0; i < data['loc'].length; i++) {
56-
console.log([data['loc'][i]['latitude'], data['loc'][i]['longitude']])
57103
link_object = window.location.origin + "/table/" + table_name + "/oid=" + data['loc'][i]['oid'] + "/";
58104
var marker = new mapboxgl.Marker()
59105
.setLngLat([data['loc'][i]['longitude'], data['loc'][i]['latitude']])

database/bupehandler/templates/bupehandler/filtered_map.html

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
<link href='/static/styles/filterBar.css' rel='stylesheet'/>
1414
<script>const table_info = {{ table_info|dict_to_json }}</script>
1515
<script src='/static/js/filterSettings.js'></script>
16+
<!-- Geocoder plugin -->
17+
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.7.0/mapbox-gl-geocoder.min.js'></script>
18+
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.7.0/mapbox-gl-geocoder.css' type='text/css' />
19+
20+
<!-- Turf.js plugin -->
21+
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script>
1622
<script>
1723
const mapParams = {
1824
table_name: "{{table_name}}",

requirements.txt

-34 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)