簡體   English   中英

如何估計緯度/經度以在地圖上顯示大概位置?

[英]how to approximate a latitude/longitude to show an approximate location on the map?

我有一個用戶的確切經度/緯度坐標(通過GPS檢索)。 但是為了保護隱私,我不想在網站上顯示精確的坐標。 我想返回在250m至500m之間隨機移動的經度/緯度。 我怎樣才能做到這一點 ?

這應該做。

var latitude = longitude = 24;

// Add offset to the coordinates
latitude += getRandomLongitudeOffset(250, 500);
longitude += getRandomLongitudeOffset(250, 500, latitude);


/* 
    Calculate one meter in degrees
    1 degree = ~111km
    1km in degree = ~0.0089
    1m in degree = ~0.0000089
*/
const COEF = 0.0000089;

/**
 * Returns an offset for coordinates in range [min, max]
*/
function getRandomLatitudeOffset(min, max){
    return getRandomInt(min, max) * COEF;
}


function getRandomLongitudeOffset(min, max, latitude){
    return (getRandomInt(min, max) * COEF) / Math.cos(latitude * 0.018);
}


/**
 * Returns a random integer between min (inclusive) and max (inclusive)
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM