Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
How to find nearest latitude/longitude from big json array of lat/lng

I'm building a website that locates your device and shows you 4 of the nearest parking meters.

For the parking meters I'm using an API to retrieve the latitude and longitude and using Google Directions API to set the start and destination coordinates and generate a route. All the parking meters are in a radius of ~2,5 km.

Now to find the 4 nearest parking meters I was thinking of running a formula and going through each record of the API to find the nearest 4. But (I think) that would take too much processing time to load into a website, therefore making it slow. There are nearly 1200 records in the API.

To calculate the route via lat/lng coordinates I'm using the following code:

fetch('https://data.stad.gent/api/records/1.0/search/?dataset=locaties-parkeerautomaten-gent&q=&rows=\
    1200&facet=parkeertariefzone&facet=bewonerszone&facet=betaalmodus&facet=status&facet=categorie')
        .then(response => response.json())
        .then(json => {
            let start = new google.maps.LatLng(51.053788, 3.730767);
            let end1 = new google.maps.LatLng(json.records[0].geometry.coordinates[1], json.records[0].geometry.coordinates[0]);
            
            let request = {
                origin: start,
                destination: end1,
                travelMode: 'WALKING'
            };
            
            let display = new google.maps.DirectionsRenderer();
            let services = new google.maps.DirectionsService();
            
            services.route(request, function (result, status) {
                if (status == 'OK') {
                    display.setDirections(result);
                }
            })
            
            let map1 = new google.maps.Map(document.getElementById("map1"));
            display.setMap(map1);

        });

QUESTION: What is in your opinion the best way to calculate and return the 4 nearest lat/lng points in an API with nearly 1200 records with a ~2,5 km radius using JavaScript?

I'm not really sure how to tackle this challenge on, any answer would be appreciated.

NOTE: It is my first question/post so if I missed something or did something stupid, do let me know, thanks in advance :)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In case anyone comes here looking for a solution, how I solved it is by the following code:

for (let i = 0; i < json.length; i++) {
                if ((Math.abs(json[i].coordinates[1] - start.lat)) + ((Math.abs(json.[i].coordinates[0] - start.lng))) < sumLatLng) {
                    closest = json[i];
                    sumLatLng = (Math.abs(json.[i].coordinates[1] - start.lat)) + ((Math.abs(json.[i].coordinates[0] - start.lng)))
                } else {
                    console.log("error");
                }
            }

Basically what I do is take the sum of the starting lat & lng, subtract the lat & lng of each record from the API and take the absolute of the result. If the result is smaller than the previous result, that means that its closer.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda