Javascript根据经度,纬度计算分数

有人可以帮我检查从地理位置计算分数的方法吗?


帮助将不胜感激


我正在计算纬度、经度的分数,但没有得到预期的结果。


请在下面查看我的代码。我不知道我做的是对还是错


这是代码片段。请运行代码并检查输出


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;

  }

预期输出:


    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

        },


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


RISEBY
浏览 131回答 1
1回答

繁星淼淼

我认为您将字符串而不是对象传递给您的计算方法。对于某些位置,分数与您要求的值不太匹配,它们显示的分数为 0.6 而不是 0.5,尽管我相信逻辑是正确的。例如,Londontowne, MD 与原点的距离为 3.82°(平均纬度/经度偏移),因此得分将为 round(10 - 3.82) = 6 / 10 = 0.6。我添加了另一种计算分数的方法,(scoreII) 只是为了好玩,它将计算与原点的距离为 0 公里的 1.00 到 1000 公里或更大的 0.00 的值。(我使用了这个出色答案中的距离公式:calculate-distance-between-two-latitude-longitude-points-havesine-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 } ];&nbsp;console.log(calculateScore(suggestions, 43.70011, -79.4163))function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {&nbsp; var R = 6371; // Radius of the earth in km&nbsp; var dLat = deg2rad(lat2-lat1);&nbsp; // deg2rad below&nbsp; var dLon = deg2rad(lon2-lon1);&nbsp;&nbsp; var a =&nbsp;&nbsp; &nbsp; Math.sin(dLat/2) * Math.sin(dLat/2) +&nbsp; &nbsp; Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *&nbsp;&nbsp; &nbsp; Math.sin(dLon/2) * Math.sin(dLon/2)&nbsp; &nbsp; ;&nbsp;&nbsp; var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));&nbsp;&nbsp; var d = R * c; // Distance in km&nbsp; return d;}function deg2rad(deg) {&nbsp; return deg * (Math.PI/180)}function calculateLocationScoreII(location, latitude, longitude) {&nbsp; &nbsp; const distanceKm = getDistanceFromLatLonInKm(location.latitude, location.longitude, latitude, longitude);&nbsp; &nbsp; let score = 1000 - distanceKm;&nbsp; &nbsp; score = score > 0 ? Math.round(score/10) / 100 : 0;&nbsp; &nbsp; return score;}function calculateLocationScore(location, latitude, longitude) {&nbsp; &nbsp; const lat = Math.abs(location.latitude - latitude);&nbsp; &nbsp; const long = Math.abs(location.longitude - longitude);&nbsp; &nbsp; let score = 10 - (lat + long) / 2;&nbsp; &nbsp; score = score > 0 ? Math.round(score) / 10 : 0;&nbsp; &nbsp; return score;}&nbsp;function calculateScore(suggestions, latitude, longitude) {&nbsp; &nbsp; // Clone the suggestions.&nbsp; &nbsp; const suggestionsResult = suggestions.map(s => { return {...s}});&nbsp; &nbsp; return suggestionsResult.map(suggestion => {&nbsp; &nbsp; &nbsp; &nbsp; suggestion.score = calculateLocationScore(suggestion, latitude, longitude);&nbsp; &nbsp; &nbsp; &nbsp; suggestion.scoreII = calculateLocationScoreII(suggestion, latitude, longitude);&nbsp; &nbsp; &nbsp; &nbsp; return suggestion;&nbsp; &nbsp; });}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript