Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中生成具有已知中心和半径的随机Lat lng值?_Javascript_Heatmap - Fatal编程技术网

如何在javascript中生成具有已知中心和半径的随机Lat lng值?

如何在javascript中生成具有已知中心和半径的随机Lat lng值?,javascript,heatmap,Javascript,Heatmap,我想在100米半径上创建一个热图层,所以我想在那个特定半径上生成随机的lat-long值,然后用 var taxiData = [ {location: new google.maps.LatLng(41.8819, -87.6278),weight: 2}, {location: new google.maps.LatLng(41.8820, -87.6279),weight: 1}, new google.maps.LatLng(41.8821, -87.6280), {location

我想在100米半径上创建一个热图层,所以我想在那个特定半径上生成随机的lat-long值,然后用

 var taxiData = [
{location: new google.maps.LatLng(41.8819, -87.6278),weight: 2},
{location: new google.maps.LatLng(41.8820, -87.6279),weight: 1},
 new google.maps.LatLng(41.8821, -87.6280),
{location: new google.maps.LatLng(41.8822, -87.6281),weight: 2},
{location: new google.maps.LatLng(41.8823, -87.6282),weight: 5},
{location: new google.maps.LatLng(41.8824, -87.6283),weight: 3},
];

请帮助

这应该允许您这样做

代码的作用:

首先,我们在三角形内生成一个相当均匀的随机点,并将其转换为半径圆内的点。然后我们将该点转换为偏移坐标并将其添加到原始坐标

返回包含两个值的对象
纬度
经度

var getRandomLocation = function (latitude, longitude, radiusInMeters) {

    var getRandomCoordinates = function (radius, uniform) {
        // Generate two random numbers
        var a = Math.random(),
            b = Math.random();

        // Flip for more uniformity.
        if (uniform) {
            if (b < a) {
                var c = b;
                b = a;
                a = c;
            }
        }

        // It's all triangles.
        return [
            b * radius * Math.cos(2 * Math.PI * a / b),
            b * radius * Math.sin(2 * Math.PI * a / b)
        ];
    };

    var randomCoordinates = getRandomCoordinates(radiusInMeters, true);

    // Earths radius in meters via WGS 84 model.
    var earth = 6378137;

    // Offsets in meters.
    var northOffset = randomCoordinates[0],
        eastOffset = randomCoordinates[1];

    // Offset coordinates in radians.
    var offsetLatitude = northOffset / earth,
        offsetLongitude = eastOffset / (earth * Math.cos(Math.PI * (latitude / 180)));

    // Offset position in decimal degrees.
    return {
        latitude: latitude + (offsetLatitude * (180 / Math.PI)),
        longitude: longitude + (offsetLongitude * (180 / Math.PI))
    }
};
var getRandomLocation=函数(纬度、经度、半径米){
var getRandomCoordinates=函数(半径,均匀){
//生成两个随机数
var a=Math.random(),
b=数学随机();
//翻转以获得更大的均匀性。
国际单项体育联合会(制服){
if(b