Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!