javascript 从纬度和经度找到一定范围内的一组坐标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3552334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding a set of coordinates within a certain range from latitude and longitide
提问by user396404
I am working on a project in javascript involving google maps.
我正在研究一个涉及谷歌地图的 javascript 项目。
The goal is to figure out 16-20 coordinate points within n kilometers from a set of latitude longitude coordinates such that the 16 points if connected will form a circle around the original coordinates.
目标是从一组纬度经度坐标中找出 n 公里范围内的 16-20 个坐标点,这样这 16 个点如果连接将围绕原始坐标形成一个圆圈。
The end goal is to make it so I can figure out coordinates to plot and connect on google maps to make a circle around a given set of coordinates.
最终目标是实现它,以便我可以找出坐标以在谷歌地图上绘制和连接,以围绕一组给定的坐标制作一个圆圈。
The code would go something like:
代码将类似于:
var coordinates = Array();
function findCoordinates(lat, long, range) {
}
coordinates = findCoordinates(-20, 40, 3);
Now to make the magic happen in the findCoordinates()function.
现在让魔法在findCoordinates()函数中发生。
回答by ConroyP
Basically what you're trying to do is find Npoints on the radius of a circle from a given point with a given radius. One simple way of doing it is splitting the 360 degrees of a circle in to Nequal chunks, and finding the points at regular intervals.
基本上,您要做的是N从给定半径的给定点在圆的半径上找到点。一种简单的方法是将一个圆的 360 度分成N相等的块,并以固定的间隔找到点。
The following should do roughly what you're after -
以下应该大致做你所追求的 -
function findCoordinates(lat, long, range)
{
// How many points do we want? (should probably be function param..)
var numberOfPoints = 16;
var degreesPerPoint = 360 / numberOfPoints;
// Keep track of the angle from centre to radius
var currentAngle = 0;
// The points on the radius will be lat+x2, long+y2
var x2;
var y2;
// Track the points we generate to return at the end
var points = [];
for(var i=0; i < numberOfPoints; i++)
{
// X2 point will be cosine of angle * radius (range)
x2 = Math.cos(currentAngle) * range;
// Y2 point will be sin * range
y2 = Math.sin(currentAngle) * range;
// Assuming here you're using points for each x,y..
p = new Point(lat+x2, long+y2);
// save to our results array
points.push(p);
// Shift our angle around for the next point
currentAngle += degreesPerPoint;
}
// Return the points we've generated
return points;
}
The array of points you get back can then easily be used to draw the circle you wish on your google map.
然后可以轻松地使用您返回的点数组在谷歌地图上绘制您想要的圆圈。
If your overall goal however is just to draw a circle at a fixed radius around a point, then a far easier solution may be to use an overlay. I've found KMBoxto be very easy to set up - you give it a central point, a radius and an image overlay (in your case, a transparent circle with a visible line around the edge) and it takes care of everything else, including resizing it on zoom in/out.
但是,如果您的总体目标只是围绕一个点以固定半径绘制一个圆,那么更简单的解决方案可能是使用叠加层。我发现KMBox非常容易设置——你给它一个中心点、一个半径和一个图像覆盖(在你的情况下,一个透明的圆圈,边缘有一条可见的线),它会处理其他一切,包括在放大/缩小时调整它的大小。
回答by Jim Tough
I had to find some code to calculate Great Circle distances a while back (just Google "Great Circle" if you don't know what I'm talking about) and I found this site:
不久前,我不得不找到一些代码来计算大圆距离(如果您不知道我在说什么,只需谷歌“大圆”),我找到了这个网站:
http://williams.best.vwh.net/gccalc.htm
http://williams.best.vwh.net/gccalc.htm
You might be able to build up your own JavaScript code to do your lat/lon range calculations using the JavaScript from that site as a reference. It sounds to me like you just need to divide up the 360 degrees of a circle into an equal number of pieces and draw a line out to an equal distance from the center at each "bearing". Once you know the lat/lon at the other end of each bearing/distance line, then connecting the dots to form a polygon is trivial.
您或许可以构建自己的 JavaScript 代码,以使用该站点中的 JavaScript 作为参考进行纬度/经度范围计算。在我看来,您只需要将一个圆的 360 度分成相等数量的部分,然后在每个“轴承”处绘制一条距中心相等距离的线。一旦您知道了每条方位/距离线另一端的纬度/经度,那么连接点以形成多边形就变得微不足道了。

