從建筑物串列中,操作員將選擇一個。
并從前端單擊它。
建筑物串列存盤在 MongoDB 資料庫中,每個建筑物都有 ax,y 點表示與 0 原點的距離。
該串列存盤在 MongoDB 資料庫中,并通過節點訪問。
每個建筑物都有 ax,y 坐標——僅樣本,而不是 geojson 資料
像 101,203 等。
貓鼬功能將:
操作員單擊按鈕并使用坐標
該功能將搜索建筑物串列并將最接近的建筑物回傳到提供的建筑物 --- 而不是最接近 0 原點的建筑物。
關于如何處理貓鼬有什么建議嗎?
謝謝!凱倫
uj5u.com熱心網友回復:
這實際上只是一個數學問題。考慮一組具有這些x,y坐標的構建檔案:
var r = [
{_id:1, x:7, y:11}
,{_id:2, x:15, y:10}
,{_id:3, x:10, y:9}
,{_id:4, x:1, y:8}
,{_id:5, x:3, y:7}
,{_id:6, x:16, y:5}
,{_id:7, x:4, y:3}
,{_id:8, x:7, y:3}
,{_id:9, x:6, y:1}
,{_id:10, x:21, y:1}
];
單擊一個按鈕并產生點6,5:
var P = {x:6, y:5};
要找到最近的建筑物,只需使用勾股定理迭代并計算距離:
db.foo.aggregate([
// Don't have to worry about absolute value of x-x1 thanks to
// pow(2) making it positive
// dist = sqrt((x-x1)^2 (y-y1)^2)
{$addFields: {dist: {$sqrt: {$add:[{$pow:[{$subtract:['$x',P.x]},2]}, {$pow:[{$subtract:['$y',P.y]},2]}]}} }},
{$sort: {'dist':1}},
{$limit: 1}
]);
{ "_id" : 8, "x" : 7, "y" : 3, "dist" : 2.23606797749979 }
洗掉$limit或將其更改為更高的數字以查看下一個最近的建筑物。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/423999.html
