将小数纬度/经度坐标转换为iPad屏幕坐标 [英] Convert decimal latitude/longitude coordinates to iPad screen coordinates
问题描述
我有一个纬度/经度小数点坐标,我想将其转换为适合iPad坐标系区域的点。
I have a latitude/longitude decimal coordinates that I would like to convert to points that will fit within the area of the iPad coordinate system.
我的代码采用纬度/经度坐标并将它们转换为笛卡尔坐标(基于这个问题:转换为笛卡尔坐标)转换结果为(2494.269287,575.376465)。
My code takes in a latitude/longitude coordinates and converts them to cartesian coordinates (based on this question: convert to cartesian coordinates) The results of the conversion are (2494.269287, 575.376465).
这是我的代码(没有编译或运行时错误):
Here is my code (no compile or runtime errors):
#define EARTH_RADIUS 6371
- (void)drawRect:(CGRect)rect
{
CGPoint latLong = {41.998035, -116.012215};
CGPoint newCoord = [self convertLatLongCoord:latLong];
NSLog(@"Cartesian Coordinate: (%f, %f)",newCoord.x, newCoord.y);
//Draw dot at coordinate
CGColorRef darkColor = [[UIColor colorWithRed:21.0/255.0
green:92.0/255.0
blue:136.0/255.0
alpha:1.0] CGColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, darkColor);
CGContextFillRect(context, CGRectMake(newCoord.x, newCoord.y, 100, 100));
}
-(CGPoint)convertLatLongCoord:(CGPoint)latLong
{
CGFloat x = EARTH_RADIUS * cos(latLong.x) * cos(latLong.y);
CGFloat y = EARTH_RADIUS * cos(latLong.x) * sin(latLong.y);
return CGPointMake(x, y);
}
如何将小数点坐标转换为将显示在iPad上的坐标屏幕?
How can I convert the decimal coordinates to coordinates that will show up on an iPad screen?
推荐答案
您的 convertLatLongCoord:
方法不会尝试调整结果指向屏幕坐标。如上所述,您可以在 -EARTH_RADIUS $范围内获得
x
和 y
值c $ c>到 + EARTH_RADIUS
。这些需要缩放以适应屏幕。
Your convertLatLongCoord:
method makes no attempt to adjust the resulting point to screen coordinates. As written, you can get x
and y
values in the range -EARTH_RADIUS
to +EARTH_RADIUS
. These need to be scaled to fit the screen.
以下内容应该有所帮助:
Something like the following should help:
- (CGPoint)convertLatLongCoord:(CGPoint)latLong {
CGFloat x = EARTH_RADIUS * cos(latLong.x) * cos(latLong.y) * SCALE + OFFSET;
CGFloat y = EARTH_RADIUS * cos(latLong.x) * sin(latLong.y) * SCALE + OFFSET;
return CGPointMake(x, y);
}
SCALE
和 OFFSET
应该是一个确定如下的值:
SCALE
and OFFSET
should be a value determined as follows:
CGSize screenSize = [UIScreen mainScreen].applicationFrame.size;
CGFloat SCALE = MIN(screenSize.width, screenSize.height) / (2.0 * EARTH_RADIUS);
CGFloat OFFSET = MIN(screenSize.width, screenSize.height) / 2.0;
这假设您希望地图填充最小的屏幕尺寸。
This assumes you want the map to fill the smallest screen dimension.
这篇关于将小数纬度/经度坐标转换为iPad屏幕坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!