Ios 如何将给定的坐标转换为“0”的边界框;直径“;x?
例如,我有一个十进制格式的纬度和经度(与纬度=44.1°9.5'30''的度数小时分钟相反)。要搜索附近的对象,我必须将搜索“半径”指定为具有四个值的矩形:Ios 如何将给定的坐标转换为“0”的边界框;直径“;x?,ios,geolocation,gps,coordinates,geocoding,Ios,Geolocation,Gps,Coordinates,Geocoding,例如,我有一个十进制格式的纬度和经度(与纬度=44.1°9.5'30''的度数小时分钟相反)。要搜索附近的对象,我必须将搜索“半径”指定为具有四个值的矩形: north = 44.1; south = -9.9; east = -22.4; west = 55.2; 是否有公式或经验法则如何将十进制lat/long值转换为矩形边界框,从而使给定的纬度/经度位于该框的中心 我必须用WGS84椭球算法来摆弄自己,还是有解决问题的开放式解决方案?如果你说的是地球上的坐标,流形上的“矩形边界框”不是真
north = 44.1;
south = -9.9;
east = -22.4;
west = 55.2;
是否有公式或经验法则如何将十进制lat/long值转换为矩形边界框,从而使给定的纬度/经度位于该框的中心
我必须用WGS84椭球算法来摆弄自己,还是有解决问题的开放式解决方案?如果你说的是地球上的坐标,流形上的“矩形边界框”不是真的没有明确的定义吗 难道你不能用笛卡尔坐标来平均矩形的尺寸来近似“盒子”的中心吗
x_center = x_left + (x_right - x_left) / 2
y_center = y_bottom + (y_top - y_bottom) / 2
为什么不从你的中心点做一个范围/方位来定义盒子角的纬度/经度(如果你是这样要求的话)?使用四个方位45度、135度、225度、315度。查看此网站的“距离/方位的终点”:我正好遇到了这个问题,解决方案并不是那么简单,但好消息是,经过大量工作(以及SO和谷歌的大量帮助),我认为我已经破解了它 有很多库,比如Proj4,它提供了大量的算法来执行所需的转换,但是我发现这一切都有点混乱,并最终编写了自己的代码(我总是想知道事情是如何工作的) 我的解决方案基于,它的工作原理如下。。。 我相信你已经弄明白了,纬度线之间的距离总是相同的(10度到20度之间的距离与20度到30度之间的距离相同),但经度线会聚在极点。因此,赤道经度10度到20度之间的距离远大于两极附近的距离(两极为0)。 所以你可以很容易地计算出两个纬度之间有多少米,但是要计算经度,你必须考虑到纬度。 在赤道附近,1度纬度与1度长的距离几乎相同,因此如果我们投影的地图有它的中心(0,0),我们可以简单地将纬度和长乘以一个常数,得到任何给定点距离地图中心的米数。 因此,我的算法有效地旋转地球,直到地图的实际中心位于0,0 所以,假设中心真的在(50.52,-4.82)-在我的例子中就是这样。 想象一下,你拿着一个地球仪,俯视它,在你正下方可见的中心有0 lat,0 long。 我们需要做的是把地球仪拿过来,它现在在我们正下方有(0,0),然后向西(向右)旋转,直到(0,-4.82)在我们下方。 然后我们将地球向南(向下)旋转,直到(50.52,-4.82)在我们下方。 作为第三步,我们可能需要顺时针或逆时针旋转它,以校正地图相对于正北的方向(如果正北在地图上是笔直的,或者如果您感兴趣的只是距离而不是方位角,则无需执行此操作) 从概念上讲,这就是我们需要做的,但这与我们的算法有什么关系呢? 答案是一个变换(类),我们在其中输入三个旋转角度。这个类有一个公共函数,给定一个lat/long对,它将在旋转后返回地球仪上该点的一个新lat/long对。 一旦我们知道了地球的半径,我们就可以把这对新的坐标转换成x和y坐标,表示距离地图原点的距离 我应该在这里提到,地球在赤道比在两极更宽,但处理这个问题的数学方法根本不值得费心。不管你怎么计算你的x,y坐标,它们都会稍微偏出,因为地球不是平的,对我来说,下面的代码就可以了。 如果你的地图非常接近极点,我怀疑这个算法的结果可能会变得非常不准确——基本上,在极点上,lat/long并不能很好地工作(从上面看一下谷歌地球) MapTransform类需要您设置一些内容。 设定半径(1000);将变换设置为使用半径为1000(单位)的球体 实体(“地球”);使用地球的平均半径(米)设置变换 设定旋转(x,y,z);将变换设置为围绕Z轴旋转Z度,Y轴旋转Y度,然后X轴旋转X度。 -基本上,给定你的中心点(lat,long)和地图上的正北是笔直向上的,你需要如下:设置旋转(0,lat,long); -旋转顺序在这里非常重要,基于坐标系(回头看你拿着的地球),其中Z轴与地球的旋转重合,Y轴上下旋转地球最近的表面,X轴是你正在看的轴-希望这有意义,这是一个很难描述的概念 鉴于您需要从一个特定点开始从lat/long映射到米,以上应该是您所需要的全部内容 函数getMapPosition(lat,long)将返回一个双精度[],其中包含以地图单位表示的x,y(如果半径以米为单位指定,则为米) 我的类在将坐标应用于特定地图分幅方面更进一步 setMapOrigin(x,y);设置贴图的旋转原点(旋转后观察者正下方的点)相对于贴图左下角的位置。名义上,这应该以米为单位(当然,如果您使用了setBody(“EARTH”);),但需要与指定半径的单位相同。 setMapSize(w,h);以米或您决定使用的任何单位设置地图的大小 最后,setBitmapSize(w,h)允许您描述要投影到的位图的大小(以像素为单位)
import java.text.DecimalFormat;
public class MapTransform {
private double circumference;
private RotationMatrix rotationMatrix;
private double originX;
private double originY;
private double mapWidth;
private double mapHeight;
private int bitmapWidth;
private int bitmapHeight;
public MapTransform() {
this.circumference = 0;
this.rotationMatrix = new RotationMatrix();
this.rotationMatrix.makeIdentity();
this.originX = 0;
this.originY = 0;
this.mapWidth = 0;
this.mapHeight = 0;
this.bitmapWidth = 0;
this.bitmapHeight = 0;
}
public void setCircumference(double circumference) {
this.circumference = circumference;
}
public void setRadius(double radius) {
this.circumference = 2 * Math.PI * radius;
}
public void setBody(String body) {
if (body.toUpperCase().equals("EARTH")) {
setRadius(6371009); //mean radius of the earth in metres
// setRadius(6378137); //equatorial radius of the earth in metres
// setRadius(6356752); //polar radius of the earth in metres
}
else {
setRadius(0);
}
}
public void setRotation(double xRotateDegrees, double yRotateDegrees, double zRotateDegrees) {
RotationMatrix xMatrix = new RotationMatrix();
RotationMatrix yMatrix = new RotationMatrix();
RotationMatrix zMatrix = new RotationMatrix();
xMatrix.makeRotateX(Math.toRadians(xRotateDegrees));
yMatrix.makeRotateY(Math.toRadians(yRotateDegrees));
zMatrix.makeRotateZ(Math.toRadians(zRotateDegrees));
this.rotationMatrix = zMatrix.concatenate(yMatrix).concatenate(xMatrix);
}
public void setMapOrigin(double originX, double originY) {
this.originX = originX;
this.originY = originY;
}
public void setMapSize(double width, double height) {
this.mapWidth = width;
this.mapHeight = height;
}
public void setBitmapSize(int width, int height) {
this.bitmapWidth = width;
this.bitmapHeight = height;
}
public double[] getMapPosition(double[] geoPosition) {
return getMapPosition(geoPosition[0], geoPosition[1]);
}
public double[] getMapPosition(double latitude, double longitude) {
// convert the GeoPosition into an NVector
NVector vec = new NVector(latitude, longitude);
// rotate the vector in 3D
vec = rotationMatrix.transform(vec);
// convert the vector into 2D units by applying circumference to latitude/longitude and adding origins
double x = vec.getLongitude() * this.circumference / 360;
double y = vec.getLatitude() * this.circumference / 360;
// return a MapPosition
return new double[] {x, y};
}
public float[] getPixelPosition(double[] mapPosition) {
return getPixelPosition(mapPosition[0], mapPosition[1]);
}
public float[] getPixelPosition(double mapX, double mapY) {
// apply origin and scale based on map and bitmap widths
float x = (float) ((this.originX + mapX) * this.bitmapWidth / this.mapWidth);
// apply origin and scale based on map and bitmap heights, but invert to measure from top left instead of bottom left
float y = (float) (this.bitmapHeight - (this.originY + mapY) * this.bitmapHeight / this.mapHeight);
return new float[] {x, y};
}
public class RotationMatrix {
String name = "";
public double array [][] = {{0,0,0},{0,0,0},{0,0,0}};
public RotationMatrix() {}
public RotationMatrix(String name) {
this.name = name;
}
public void makeIdentity() {
for(int x = 0; x <= 2; x++) {
for (int y = 0; y <= 2; y++) {
array[x][y] = (x == y)? 1: 0;
}
}
}
public void makeRotateX(double thetaRadians) {
double cosTheta = Math.cos(thetaRadians);
double sinTheta = Math.sin(thetaRadians);
makeIdentity();
array[1][1] = cosTheta;
array[2][1] = -sinTheta;
array[1][2] = sinTheta;
array[2][2] = cosTheta;
}
public void makeRotateY(double thetaRadians) {
double cosTheta = Math.cos(thetaRadians);
double sinTheta = Math.sin(thetaRadians);
makeIdentity();
array[0][0] = cosTheta;
array[2][0] = sinTheta;
array[0][2] = -sinTheta;
array[2][2] = cosTheta;
}
public void makeRotateZ(double thetaRadians) {
double cosTheta = Math.cos(thetaRadians);
double sinTheta = Math.sin(thetaRadians);
makeIdentity();
array[0][0] = cosTheta;
array[1][0] = -sinTheta;
array[0][1] = sinTheta;
array[1][1] = cosTheta;
}
public NVector transform(NVector vec) {
NVector vec2 = new NVector();
vec2.x = vec.x * array[0][0] + vec.y * array[1][0] + vec.z * array[2][0];
vec2.y = vec.x * array[0][1] + vec.y * array[1][1] + vec.z * array[2][1];
vec2.z = vec.x * array[0][2] + vec.y * array[1][2] + vec.z * array[2][2];
return vec2;
}
public void output() {
if (this.name != null && this.name.length() == 0) {
System.out.println(this.name + "-------");
}
DecimalFormat df = new DecimalFormat("0.00");
for(int y = 0; y <= 2; y++) {
String out = "| ";
double test = 0;
for(int x = 0; x <= 2; x++) {
String f = df.format(array[x][y]);
if (f.length() < 5) f = " " + f;
out += f + " ";
test = test + array[x][y] * array[x][y];
}
if (test > 0.99 && test < 1.01) {test = 1.0;}
out += "| (=" + test + ")";
System.out.println(out);
}
System.out.println();
}
public RotationMatrix concatenate(RotationMatrix m2) {
RotationMatrix outputMatrix = new RotationMatrix();
for(int x = 0; x <= 2; x++) {
for(int y = 0; y <=2; y++) {
outputMatrix.array[x][y] = 0;
for (int q = 0; q <= 2; q++) {
outputMatrix.array[x][y] += this.array[x][q] * m2.array[q][y];
}
}
}
return outputMatrix;
}
}
public class NVector {
double x;
double y;
double z;
public NVector() {
this.x = 0;
this.y = 0;
this.z = 0;
}
public NVector(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public NVector(double latitude, double longitude) {
setLatitudeLongitude(latitude, longitude);
}
public NVector(double[] geoPosition) {
setLatitudeLongitude(geoPosition[0], geoPosition[1]);
}
private void setLatitudeLongitude(double latitude, double longitude) {
double latitudeRadians = Math.toRadians(latitude);
double longitudeRadians = Math.toRadians(longitude);
double cosLatitude = Math.cos(latitudeRadians);
double cosLongitude = Math.cos(longitudeRadians);
double sinLatitude = Math.sin(latitudeRadians);
double sinLongitude = Math.sin(longitudeRadians);
this.x = cosLatitude * cosLongitude;
this.y = cosLatitude * sinLongitude;
this.z = sinLatitude;
}
public double getLatitude() {
return Math.toDegrees(Math.atan2(this.z, Math.sqrt(this.x * this.x + this.y * this.y)));
}
public double getLongitude() {
return Math.toDegrees(Math.atan2(this.y, this.x));
}
public double[] getGeoPosition() {
double[] geoPosition = new double[] {this.getLatitude(), this.getLongitude()};
return geoPosition;
}
public void output() {
output("");
}
public void output(String name) {
if (name != null && name.length() == 0) {
System.out.println("NVector: " + name);
}
DecimalFormat df = new DecimalFormat("0.00");
String vector = df.format(this.x) + "," + df.format(this.y) + "," + df.format(this.z);
String coords = "";
try {
coords = df.format(Math.toDegrees(this.getLatitude())) + "N " + df.format(Math.toDegrees(this.getLongitude())) + "E";
}
catch(Exception e) {
coords = "(coords unknown)";
}
System.out.println("(" + vector + ") at " + coords);
}
}
}
public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
double earthRadius = 6371009; //mean radius of the earth in metres
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
return dist;
}