Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
How to find the coordinate that is closest to the another coordinate? (JavaScript)

We have a list of many waypoints containing Latitude and Longitude.

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}

How can we find the closest waypoint for this point

{type:'user_device', lat: '59.37441', lng: '24.71794'}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I've already found an answer here: 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
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda