Javascript 如何近似纬度/经度以在地图上显示近似位置?
我有一个用户的准确经纬度坐标(通过GPS检索)。但是为了保护隐私,我不想在网站上显示精确的坐标。我想返回在250米到500米之间随机移动的经度/纬度。我怎样才能做到这一点?这应该可以Javascript 如何近似纬度/经度以在地图上显示近似位置?,javascript,google-maps,geolocation,geospatial,geocoding,Javascript,Google Maps,Geolocation,Geospatial,Geocoding,我有一个用户的准确经纬度坐标(通过GPS检索)。但是为了保护隐私,我不想在网站上显示精确的坐标。我想返回在250米到500米之间随机移动的经度/纬度。我怎样才能做到这一点?这应该可以 var latitude = longitude = 24; // Add offset to the coordinates latitude += getRandomLongitudeOffset(250, 500); longitude += getRandomLongitudeOffset(250, 50
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;
}