Convert decimal latitude/longitude coordinates to iPad screen coordinates

落花浮王杯 提交于 2019-11-29 22:58:20

问题


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.

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

How can I convert the decimal coordinates to coordinates that will show up on an iPad screen?


回答1:


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



来源:https://stackoverflow.com/questions/16124171/convert-decimal-latitude-longitude-coordinates-to-ipad-screen-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!