Localization of user points with Haversine (Javascript) - How to improve code (or logic)?

Asked

Viewed 12 times

0

Hello. I’m trying to recreate a program that I did some time ago, where one of the functions is:

  1. Insert coordinates in geodetic decimal format (Ex: -30,023927)
  2. Locate the three closest points to the previously inserted coordinates
  3. Display the next three dots
    • Display data for each point
    • Display the distance between the inserted coordinates and the points
  • Points are stored in an array like this:
const arr = [
    {
     ID: "1",
     code: "1",
     nome: "RODOVIÁRIA",
     telefone: "3221-9371",
     logradouro: "LG VESPASIANO JULIO VEPPO",
     number: "0",
     latitude: "-30.02399616",
     longitude: "-51.21945127"
    },
    {
     ID: "2",
     code: "458",
     nome: "AV. DAS INDÚSTRIAS X AV. SEVERO DULLIUS",
     telefone: "",
     logradouro: "AV. DAS INDUSTRIAS",
     number: "1344",
     latitude: "-29.98727767",
     longitude: "-51.16983678"
    },
    {
     ID: "3",
     code: "327",
     nome: "DOM PEDRO II X CORCOVADO",
     telefone: "",
     logradouro: "R DOM PEDRO II",
     number: "1551",
     latitude: "-30.0206083",
     longitude: "-51.18628405"
    }]
  • The distance between the coordinates is calculated by Haversine’s formula:
function haversine() {
    var radians = Array.prototype.map.call(arguments, function(deg) { return deg/180.0 * Math.PI; });
    var lat1 = radians[0], lon1 = radians[1], lat2 = radians[2], lon2 = radians[3];
    var R = 6372.8; // km
    var dLat = lat2 - lat1;
    var dLon = lon2 - lon1;
    var a = Math.sin(dLat / 2) * Math.sin(dLat /2) + Math.sin(dLon / 2) * Math.sin(dLon /2) * Math.cos(lat1) * Math.cos(lat2);
    var c = 2 * Math.asin(Math.sqrt(a));
    var d = R * c
    return d
}

My previous solution was based on the following logic:

  1. Creating a new array (distanceArray) with only the distances between the inserted coordinates and the points of the previous array (arr)
  2. Creating a new array (distanceArraySort), where the distanceArray is ordered in ascending order (from the shortest distance to the longest)
  3. The creation of a search function (searchIndex), where:
    • At every iteration (for), the three lowest values (distances) in distanceArraySort are stored in the variable Pindex.
      • By means of the method findIndex(), values stored are compared with values in distanceArray, returning the index of the found values.
  • Imagine the following case:

arr

index Name
0 Point A
1 Point B
2 Point C

distanceArray

index Distance
0 300
1 100
2 200

distanceArraySort

index Distance
0 100
1 200
2 300
  1. The method findIndex() compare distanceArraySort[0] with distanceArray and returns the location (index) of value pIndex in distanceArray.
  2. After the index is found, a console.log(arr[index, pIndex)
console.log(arr[index], "Distância:", pIndex) // index = 1 e pIndex = 100
// saída esperada: Ponto B Distância: 100
  • Unfortunately, I’m a beginner in programming and that was the conclusion I came to after a few days researching and thinking. The question is, what other methods could I have used to accomplish the goal more efficiently? 'Cause to be perfectly honest, I feel like I’ve done a gigantic stunt.
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.