admin管理员组文章数量:1394052
i want to create a heatmap layer over a 100 meters radius and so i want to generate random lat-long values in that specific radius and so want to replace it with
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},
];
please help
i want to create a heatmap layer over a 100 meters radius and so i want to generate random lat-long values in that specific radius and so want to replace it with
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},
];
please help
Share Improve this question asked Aug 17, 2014 at 15:44 nish soninish soni 411 silver badge3 bronze badges1 Answer
Reset to default 10This should allow you to do that.
What the code does:
First we generate a fairly uniform random point within a triangle and convert that to a point within the circle of a radius. Then we convert that point to offset coordinates and add them to the original.
An object containing two values latitude
and longitude
is returned.
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))
}
};
本文标签: heatmaphow to generate random Latlng values with known center and radius in javascriptStack Overflow
版权声明:本文标题:heatmap - how to generate random Lat-lng values with known center and radius in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744086083a2588545.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论