Tenemos una lista de muchos waypoints que contienen Latitud y Longitud.
0: {order: '1', lat: '59.36517', lng: '24.71795', type: 'waypoint', distance: 8225.839271423867} 1: {order: '2', lat: '59.36683', lng: '24.71798', type: 'waypoint', distance: 8047.60794553467} 2: {order: '3', lat: '59.36955', lng: '24.71765', type: 'waypoint', distance: 7762.290773339123} 3: {order: '4', lat: '59.37130', lng: '24.71726', type: 'waypoint', distance: 7582.402618060507} 4: {order: '5', lat: '59.37235', lng: '24.71759', type: 'waypoint', distance: 7465.330388651929} 5: {order: '6', lat: '59.37258', lng: '24.71761', type: 'waypoint', distance: 7440.570044348184} 6: {order: '7', lat: '59.37350', lng: '24.71812', type: 'waypoint', distance: 7334.306732704424} 7: {order: '8', lat: '59.37383', lng: '24.71796', type: 'waypoint', distance: 7301.993261275825} 8: {order: '9', lat: '59.37521', lng: '24.71792', type: 'waypoint', distance: 7156.449114689986}¿Cómo podemos encontrar el waypoint más cercano a este punto?
{type:'user_device', lat: '59.37441', lng: '24.71794'}Ya encontré una respuesta aquí: https://www.sitepoint.com/community/t/find-record-with-closest-latitude-longitude-from-stringifyed-data-in-localstorage/23845
function closestLocation(targetLocation, locationData) { function vectorDistance(dx, dy) { return Math.sqrt(dx * dx + dy * dy); } function locationDistance(location1, location2) { var dx = location1.latitude - location2.latitude, dy = location1.longitude - location2.longitude; return vectorDistance(dx, dy); } return locationData.reduce(function(prev, curr) { var prevDistance = locationDistance(targetLocation , prev), currDistance = locationDistance(targetLocation , curr); return (prevDistance < currDistance) ? prev : curr; }); } var data = [ { "latitude": "57.6494", "longitude": "-3.5606" }, { "latitude": "57.077", "longitude": "-2.836" }, ], targetLocation = {latitude: 56, longitude: -5 }, closest = closestLocation(targetLocation, data); //closest is now the location that is closest to the target location