在纬度和经度的某个范围内查找一组坐标

时间:2010-08-23 22:55:40

标签: javascript google-maps coordinates latitude-longitude

我正在开发一个涉及谷歌地图的javascript项目。

目标是在一组纬度经度坐标的n公里范围内找出16-20个坐标点,这样如果连接的16个点将围绕原始坐标形成一个圆。

最终目标是让我能够找到坐标以绘制并连接谷歌地图,围绕给定的一组坐标创建一个圆圈。

代码将类似于:

var coordinates = Array();
function findCoordinates(lat, long, range) {
}
coordinates = findCoordinates(-20, 40, 3);

现在让魔术发生在findCoordinates()函数中。

2 个答案:

答案 0 :(得分:7)

基本上你要做的是在给定半径的给定点的圆的半径上找到N个点。一种简单的方法是将360度的圆圈分成N个相等的块,然后定期查找点。

以下内容应该大致完成你所追求的目标 -

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

然后,您可以轻松地使用您获得的点数组在Google地图上绘制您想要的圆圈。

如果您的总体目标只是在一个点周围的固定半径绘制一个圆,那么更容易的解决方案可能是使用叠加。我发现KMBox很容易设置 - 你给它一个中心点,一个半径和一个图像叠加(在你的情况下,一个透明的圆圈,边缘有一条可见的线条),它需要小心其他一切,包括在放大/缩小时调整大小。

答案 1 :(得分:1)

我不得不找到一些代码来计算Great Circle距离一段时间(如果你不知道我在说什么,那就是谷歌“Great Circle”)我找到了这个网站:

http://williams.best.vwh.net/gccalc.htm

您可以使用该网站的JavaScript作为参考,建立自己的JavaScript代码来进行纬度/经度范围计算。听起来像你只需要将360度的圆圈分成相同数量的棋子,并在每个“方位”处画一条距离中心相等的距离。一旦你知道每个方位/距离线另一端的纬度/经度,那么连接点形成一个多边形是微不足道的。