【问题标题】:how to approximate a latitude/longitude to show an approximate location on the map?如何近似纬度/经度以在地图上显示大致位置?
【发布时间】:2018-05-01 21:25:48
【问题描述】:

我有一个用户的精确经度/纬度坐标(通过 GPS 检索)。但为了保护隐私,我不想在网站上显示精确的坐标。我想返回在 250m 到 500m 之间随机移动的经度/纬度。我该怎么做?

【问题讨论】:

    标签: javascript google-maps geolocation geospatial geocoding


    【解决方案1】:

    应该这样做。

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-13
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多