Search code examples
javascriptnode.jsgeolocationgeo

Javascript Calculate Score From Longitude, Latitude


Can someone help me to check the way to calculate a score from geolocation?

Help will be appreciated

I am calculating the score from latitude, longitude but not getting the expected result.

Please see my code below. I don't know I am doing right or wrong

Here is the snippet of code. Please run the code and check the output

const suggestions = `{
suggestions: [
{
name: "London, ON, Canada",
latitude: 42.98339,
longitude: -81.23304
},
{
name: "Londontowne, MD, USA",
latitude: 38.93345,
longitude: -76.54941
},
{
name: "London, OH, USA",
latitude: 39.88645,
longitude: -83.44825
},
{
name: "Londonderry, NH, USA",
latitude: 42.86509,
longitude: -71.37395
},
{
name: "New London, CT, USA",
latitude: 41.35565,
longitude: -72.09952
},
{
name: "New London, WI, USA",
latitude: 44.39276,
longitude: -88.73983
},
{
name: "London, KY, USA",
latitude: 37.12898,
longitude: -84.08326
}
]
}`;

console.log(calculateScore(suggestions, 43.70011, -79.4163))
function calculateScore(suggestions, latitude, longitude) {
    const suggestionsResult = suggestions;
    for (let i = 0; i < suggestionsResult.length; i += 1) {
      let score = 0;
      const lat = Math.abs(suggestionsResult[i].latitude - latitude);
      const long = Math.abs(suggestionsResult[i].longitude - longitude);

      score = 10 - (lat + long) / 2;
      score = score > 0 ? Math.round(score) / 10 : 0;

      suggestionsResult[i].score = score;
    }
    return suggestionsResult;
  }
  
  

Expected output :

    suggestions: [{
            name: "London, ON, Canada",
            latitude: 42.98339,
            longitude: -81.23304,
            score: 0.9
        },
        {
            name: "Londontowne, MD, USA",
            latitude: 38.93345,
            longitude: -76.54941,
            score: 0.5
        },
        {
            name: "London, OH, USA",
            latitude: 39.88645,
            longitude: -83.44825,
            score: 0.5
        },
        {
            name: "Londonderry, NH, USA",
            latitude: 42.86509,
            longitude: -71.37395,
            score: 0.5
        },
        {
            name: "New London, CT, USA",
            latitude: 41.35565,
            longitude: -72.09952,
            score: 0.5
        },
        {
            name: "New London, WI, USA",
            latitude: 44.39276,
            longitude: -88.73983,
            score: 0.5
        },
        {
            name: "London, KY, USA",
            latitude: 37.12898,
            longitude: -84.08326,
            score: 0.4
        }
    ]
}```

Please help me to check the above code. I am writing correctly.

Solution

  • I think you were passing a string instead of an object to your calculation method.

    The scores don't quite match up to your required values for some of the locations, they are showing a score of 0.6 rather than 0.5, though I believe the logic is correct.

    For example, Londontowne, MD has a distance of 3.82° (average offset lat/long) from the origin point, so the score will be round(10 - 3.82) = 6 / 10 = 0.6.

    I've added another way of calculating scores, (scoreII) just for fun that will calculate the distance from the origin as a value of 1.00 for 0km to 0.00 for 1000km or greater. (I've used the distance formula from this excellent answer: calculate-distance-between-two-latitude-longitude-points-haversine-formula)

    const suggestions = [ { name: "London, ON, Canada", latitude: 42.98339, longitude: -81.23304 }, { name: "Londontowne, MD, USA", latitude: 38.93345, longitude: -76.54941 }, { name: "London, OH, USA", latitude: 39.88645, longitude: -83.44825 }, { name: "Londonderry, NH, USA", latitude: 42.86509, longitude: -71.37395 }, { name: "New London, CT, USA", latitude: 41.35565, longitude: -72.09952 }, { name: "New London, WI, USA", latitude: 44.39276, longitude: -88.73983 }, { name: "London, KY, USA", latitude: 37.12898, longitude: -84.08326 } ]; 
    
    console.log(calculateScore(suggestions, 43.70011, -79.4163))
    
    function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
      var R = 6371; // Radius of the earth in km
      var dLat = deg2rad(lat2-lat1);  // deg2rad below
      var dLon = deg2rad(lon2-lon1); 
      var a = 
        Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
        Math.sin(dLon/2) * Math.sin(dLon/2)
        ; 
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      var d = R * c; // Distance in km
      return d;
    }
    
    function deg2rad(deg) {
      return deg * (Math.PI/180)
    }
    
    function calculateLocationScoreII(location, latitude, longitude) {
        const distanceKm = getDistanceFromLatLonInKm(location.latitude, location.longitude, latitude, longitude);
        let score = 1000 - distanceKm;
        score = score > 0 ? Math.round(score/10) / 100 : 0;
        return score;
    }
    
    function calculateLocationScore(location, latitude, longitude) {
        const lat = Math.abs(location.latitude - latitude);
        const long = Math.abs(location.longitude - longitude);
        let score = 10 - (lat + long) / 2;
        score = score > 0 ? Math.round(score) / 10 : 0;
        return score;
    }
     
    function calculateScore(suggestions, latitude, longitude) {
        // Clone the suggestions.
        const suggestionsResult = suggestions.map(s => { return {...s}});
        return suggestionsResult.map(suggestion => {
            suggestion.score = calculateLocationScore(suggestion, latitude, longitude);
            suggestion.scoreII = calculateLocationScoreII(suggestion, latitude, longitude);
            return suggestion;
        });
    }