Javascript - 计算围绕中心点的四个角的地理坐标点
Posted
技术标签:
【中文标题】Javascript - 计算围绕中心点的四个角的地理坐标点【英文标题】:Javascript - Calcualate the geo-coordinate points of four corners around a center point 【发布时间】:2015-10-20 08:47:17 【问题描述】:我有一个区域的中心点坐标 (lng, lat),我想计算围绕该中心点的边界框中四个角的坐标 (lng, lat)。从中心到每个角落的距离是10米。
插图:
如何在 javascript 中执行此操作?
是否有这样的图书馆,或者我必须掸掉我多年来忽视阿拉德夫人三角学课的大脑部分?
【问题讨论】:
这个library 可能对你很感兴趣。但无论如何,我已经更新了我的答案来解决你的具体问题。 我查看了那个库,但它没有我需要的解决方案。谢谢。 【参考方案1】:根据 Bruno Pinto here 给出的答案 我最终创建并使用了它:
function getBoundingBox(pLatitude, pLongitude, pDistanceInMeters)
var latRadian = pLatitude.toRad();
var degLatKm = 110.574235;
var degLongKm = 110.572833 * Math.cos(latRadian);
var deltaLat = pDistanceInMeters / 1000.0 / degLatKm;
var deltaLong = pDistanceInMeters / 1000.0 / degLongKm;
var topLat = pLatitude + deltaLat;
var bottomLat = pLatitude - deltaLat;
var leftLng = pLongitude - deltaLong;
var rightLng = pLongitude + deltaLong;
var northWestCoords = topLat + ',' + leftLng;
var northEastCoords = topLat + ',' + rightLng;
var southWestCoords = bottomLat + ',' + leftLng;
var southEastCoords = bottomLat + ',' + rightLng;
var boundingBox = [northWestCoords, northEastCoords, southWestCoords, southEastCoords];
return boundingBox;
if (typeof(Number.prototype.toRad) === "undefined")
Number.prototype.toRad = function()
return this * Math.PI / 180;
【讨论】:
【参考方案2】:你可以看看下面的函数:-
/*The getRectCoordinates takes the coordinates of the center point
and the radial distance at which you want to find the coordinates*/
function getRectCoordinates (x,y,r)
//theta is the angle hard-coded to 45 degrees to think as the square
var theta = Math.PI/4,
cos = Math.cos,
sin = Math.sin;
return
'topRight' :
'x' : x + r*cos(theta),
'y' : y + r*sin(theta)
,
'bottomRight' :
'x' : x + r*cos(theta),
'y' : y - r*sin(theta)
,
'topLeft' :
'x' : x - r*cos(theta),
'y' : y + r*sin(theta)
,
'bottomLeft' :
'x' : x + r*cos(theta),
'y' : y - r*sin(theta)
//An example for this (Check console for output):
console.log(getRectCoordinates(2,3,10));
如果您的问题解决了,请告诉我。
【讨论】:
【参考方案3】:设(x, y)
为中心坐标,如下图所示:
那么你实际上可以通过以下方式计算坐标:
/* Returns an array of arrays, each one corresponding to each point's
coordinates (x, y) */
function getCoordinatesSquare (distance_to_center, x, y)
var points = [];
var dx = distance_to_center * Math.sin(Math.PI / 4);
var dy = distance_to_center * Math.cos(Math.PI / 4);
points[0] = [x - dx, y - dy]; // (x1, y1)
points[1] = [x - dx, y + dy]; // (x2, y2)
points[2] = [x + dx, y + dy]; // (x3, y3)
points[3] = [x + dx, y - dy]; // (x4, y4)
return points;
/* Set the initial conditions. */
var distance_to_center = 10;
var x = 4;
var y = 5;
/* Calculate. */
var coordinates = getCoordinatesSquare (distance_to_center, x, y);
/* Test the results. */
for (var i = 0; i < coordinates.length; i++)
console.log ("(" + coordinates[i][0] + ", " + coordinates[i][1] + ") \n");
在这种情况下,输出将是:
(-3.0710678118654746, -2.0710678118654755)
(-3.0710678118654746, 12.071067811865476)
(11.071067811865476, 12.071067811865476)
(11.071067811865476, -2.0710678118654755)
并绘制点:
【讨论】:
这没有得到我需要的结果..可能是由于距离没有被转换 @Zigglzworth 您需要什么结果?我知道你在问如何计算一个正方形的坐标给定它的中心位置......你所说的距离转换到底是什么? 我的问题特别适用于地理坐标和以米为单位的距离。在使用一组真实坐标应用您的解决方案时,我发现生成的 4 个坐标不正确(远离中心点)。我认为这是由于距离转换问题以上是关于Javascript - 计算围绕中心点的四个角的地理坐标点的主要内容,如果未能解决你的问题,请参考以下文章