I am working on a school project where I have to create a Web Page where I should be able to use leaflet to create a route. I also have a JSON file with some Bike Docks. I need to find the nearest dock from the starting point of my trip and the nearest from the ending point of the route.
This is my code so far:
<script>
var map_var = L.map('map_id').setView([40.72730240765651, -73.9939667324035], 16);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map_var);
var startPosition = L.latLng();
var endPosition = L.latLng();
L.Routing.control({
waypoints: [
startPosition,
endPosition,
],
routeWhileDragging: true,
collapsible: true,
geocoder: L.Control.Geocoder.nominatim()
}).addTo(map_var);
$.getJSON("citybike.json", function (json1) {
for (var i = 0; i < json1.stationBeanList.length; i++) {
var place = json1.stationBeanList[i];
coordinate = [place.latitude, place.longitude];
/*var marker = L.marker(coordinate);
marker.addTo(map_var).bindPopup(`<b>${place.stAddress1}</b><br>Status: ${place.statusValue}<br>Bici disponibili: ${place.availableBikes}<br>Docks disponibili: ${place.availableDocks}`);*/
}
});
</script>
My JSON file is like this:
{
"stationBeanList": [
{
"id": 72,
"stationName": "W 52 St & 11 Ave",
"availableDocks": 32,
"totalDocks": 39,
"latitude": 40.76727216,
"longitude": -73.99392888,
"statusValue": "In Service",
"statusKey": 1,
"availableBikes": 6,
"stAddress1": "W 52 St & 11 Ave",
"stAddress2": "",
"city": "",
"postalCode": "",
"location": "",
"altitude": "",
"testStation": false,
"lastCommunicationTime": null,
"landMark": ""
}
}
Thank you for your help!