json 用瓣叶中的坐标查找最近点

sd2nnvve  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(176)

我正在做一个学校项目,我必须创建一个网页,我应该能够使用传单创建一个路线。我也有一个JSON文件与一些自行车码头。我需要找到离我旅行的起点最近的码头和离路线的终点最近的码头。
这是我目前为止的代码:

<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: '&copy; <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>

我的JSON文件是这样的:

{
    "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": ""
        }
}

谢谢你的帮助!

x6yk4ghg

x6yk4ghg1#

您需要迭代数组中的每个站点并返回-给定一组起始lat/lng坐标,以及站点自己的lat/lng坐标集-两对之间的距离。我用的是this algorithm to good effect
为了方便起见,我已经将站点数据缩减到最小,并引入了几个附加站点。我给每个人都编了坐标。

// Station dataset, and the the coordinates
// that we want to measure from
const stations={stationBeanList:[{id:72,stationName:"W 52 St & 11 Ave",availableDocks:32,totalDocks:39,latitude:40.76727216,longitude:-73.99392888,availableBikes:7},{id:76,stationName:"1st & Houston",availableDocks:23,totalDocks:30,latitude:41.76727216,longitude:-74.99392888,availableBikes:7},{id:12,stationName:"White plains",availableDocks:12,totalDocks:22,latitude:51.76727216,longitude:1.99392888,availableBikes:10}]};    const locationLat = 40.72730240765651;
const locationLng = -73.9939667324035;

// Our key distance function which takes in the coordinates
// from our current position, a set of station coordinates,
// and a unit of measurement (KM or M)
function getDistance(lat1, lon1, lat2, lon2, unit = 'M') {
  
  const R = 6371e3; // metres
  const φ1 = lat1 * Math.PI/180; // φ, λ in radians
  const φ2 = lat2 * Math.PI/180;
  const Δφ = (lat2-lat1) * Math.PI/180;
  const Δλ = (lon2-lon1) * Math.PI/180;

  const a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
            Math.cos(φ1) * Math.cos(φ2) *
            Math.sin(Δλ/2) * Math.sin(Δλ/2);
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

  const d = R * c; // in metres

  if (unit === 'km') return d * 0.001;
  if (unit === 'mi') return d * 0.0006213712;

}

// This function accepts the initial coordinates, the stations list
// and the unit of measurement
function getStationDistances(locLat, locLng, stationsList, unit = 'mi') {
  
  // Iterate over the stations list using `map` which
  // will return a new array of objects
  return stationsList.map(station => {
    
    // For each station destructure the properties we
    // want to process (and return)
    const {
      id,
      stationName: name,
      latitude: sLat,
      longitude: sLng,
      availableDocks
    } = station; 
    
    // Pass the initial coordinates, the stations coordinates, and
    // a unit of measurement into the `getDistance` function.
    // Set the resulting value to two decimal places, and then
    // parse it back to a floating-point number
    const distance = Number.parseFloat((Math.round(getDistance(
      locLat,
      locLng,
      sLat,
      sLng,
      unit) * 100) / 100).toFixed(2));
    
    // In this example we're returning the station id, name,
    // the distance from the initial location, the unit of
    // measurement, its coordinates, and its available docks.
    return {
      id,
      name,
      distance,
      unit,
      sLat,
      sLng,
      availableDocks
    };
  
  // Finally we're sorting the returned array of objects by
  // distance starting with the nearest one
  }).sort((a, b) => a.distance > b.distance);

}

console.log(getStationDistances(locationLat, locationLng, stations.stationBeanList, 'mi'));

相关问题