在纬度和长度的一定范围内找到一组坐标 [英] Finding a set of coordinates within a certain range from latitude and longitide

查看:105
本文介绍了在纬度和长度的一定范围内找到一组坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们的目标是从一组纬度经度中计算出n个公里内的16-20个坐标点坐标,如果连接16个点将围绕原始坐标形成一个圆圈。



最终目标是让我能够计算出坐标来绘制和连接谷歌地图围绕一组给定的坐标做一个圆圈。



代码会像这样:

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

现在让魔法在 findCoordinates()函数。

解决方案

基本上你要做的是找到 N 从具有给定半径的给定点开始的圆的半径上的点。一种简单的方法是将圆的360度分成 N 等分块,并定期查找点。



以下内容应该可以大致说明您的情况 -

 函数findCoordinates(lat,long,range)
{
//我们需要多少点? (应该可能是函数参数..)
var numberOfPoints = 16;
var degreesPerPoint = 360 / numberOfPoints;

//跟踪从中心到半径的角度
var currentAngle = 0;

//半径上的点将为lat + x2,long + y2
var x2;
var y2;
//跟踪我们生成的要返回的点
var points = [];

for(var i = 0; i< numberOfPoints; i ++)
{
// X2点将是角度*半径(范围)的余弦
x2 = Math.cos(currentAngle)*范围;
// Y2点将是sin *范围
y2 = Math.sin(currentAngle)*范围;

//假设您在这里为每个x,y使用点数
p = new Point(lat + x2,long + y2);

//保存到我们的结果数组
points.push(p);

//将我们的角度转到下一个点
currentAngle + = degreesPerPoint;
}
//返回我们生成
返回点的点;

$ / code>

您可以很容易地使用返回的点阵来绘制圆你希望在你的谷歌地图上。



如果你的总体目标是围绕一个点在一个固定的半径上绘制一个圆,那么更简单的解决方案可能是使用覆盖。我发现 KMBox 很容易设置 - 你给它一个中央点,一个半径和一个图像叠加(在你的情况下,一个透明的圆圈在边缘有一条可见的线条),它负责处理所有其他事情,包括在放大/缩小时调整大小。


I am working on a project in javascript involving google maps.

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.

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.

解决方案

Basically what you're trying to do is find N points 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 N equal chunks, and finding the points at regular intervals.

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 KMBox to 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.

这篇关于在纬度和长度的一定范围内找到一组坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆