亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript根據經度,緯度計算分數

Javascript根據經度,緯度計算分數

RISEBY 2022-01-13 15:17:01
有人可以幫我檢查從地理位置計算分數的方法嗎?幫助將不勝感激我正在計算緯度、經度的分數,但沒有得到預期的結果。請在下面查看我的代碼。我不知道我做的是對還是錯這是代碼片段。請運行代碼并檢查輸出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.
查看完整描述

1 回答

?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

我認為您將字符串而不是對象傳遞給您的計算方法。


對于某些位置,分數與您要求的值不太匹配,它們顯示的分數為 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 } ]; 


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;

    });

}

 

查看完整回答
反對 回復 2022-01-13
  • 1 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號