将地理坐标转换为custon = m UIView上的CGPoint

时间:2013-11-04 14:11:06

标签: objective-c uiimageview coordinate-systems cgpoint coordinate-transformation

我正在构建一个应用程序,其中一个功能将在自定义UIImageView上推送地理坐标。我的数学很差,所以我似乎无法得到正确的价值观。这就是我正在做的事情:

我的图像是2048x2048,我把它放在UIScrollView中,当我得到坐标时,让我们说“悉尼-33.856963,151.215219”我把它们变成UIView坐标(x, y)的

- (void)viewDidLoad
{
    [super viewDidLoad];
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    scrollView.delegate = self;
    scrollView.showsVerticalScrollIndicator = YES;
    scrollView.scrollEnabled = YES;
    scrollView.userInteractionEnabled = YES;
    [scrollView setBounces:NO];
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 100.0;


    mainImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, IMAGE_SIZE, IMAGE_SIZE)];
    mainImageView.image = [UIImage imageNamed:@"map.jpg"];
    mainImageView.contentMode = UIViewContentModeScaleAspectFit;
    tipScroll.contentSize = CGSizeMake(IMAGE_SIZE, IMAGE_SIZE);


    [scrollView addSubview:mainImageView];
    [self.view addSubview:scrollView];

    NSString* fileContents = @"-33.856963,151.215219";
    NSArray* pointStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    for (int i = 0; i<[pointStrings count]; i++) {
        NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
        [self getCoordinates:latLonArr];

    }

 }  
-(void)getCoordinates:(NSArray *)latLonArr{

    double comparatorWidth = IMAGE_SIZE/360.0f;
    double comparatorHeight = IMAGE_SIZE/180.0f;


    double Xl = [[latLonArr objectAtIndex:1] doubleValue]; //151.215219
    double Yl = [[latLonArr objectAtIndex:0] doubleValue]; //-33.856963

    coordX = (Xl+180.0f)*comparatorWidth;
    coordY = (90.0f-Yl)*comparatorHeight;

    UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"star.png"]];

    imageView=[[UIImageView alloc] init];

    [imageView setFrame:CGRectMake(coordX,coordY,10,10)];

    [imageView setImage:image];
    [scrollView addSubview:imageView];
}

我从中心坐标(0,0)越远,点越不准确。如果我有一个西非城市的坐标,它将是正确的,但悉尼很多。我怎样才能解决这个问题?

Sydney image

2 个答案:

答案 0 :(得分:2)

我认为问题在于地球不平坦。这意味着您无法将地理坐标简单地转换为视图的二维系统。 http://en.wikipedia.org/wiki/Geographic_coordinate_system

检查此问题和正确答案: Converting longitude/latitude to X/Y coordinate

答案 1 :(得分:1)

查看您发布的图像上的白色水平线。它们没有均匀间隔 - 它们朝图像底部变宽。这意味着您的地图图片不是使用Equirectangular projection制作的,而且可能是Mercator projection图片。

您发布的代码仅通过偏移和缩放将lat / long转换为Y / X只适用于Equirectangular投影图像。

对于墨卡托投影,转换更复杂。请参阅Covert latitude/longitude point to a pixels (x,y) on mercator projection

所以你有两个选择:

A)使用equirectangular投影地图图像

B)继续使用墨卡托地图图像,并修复你的纬度/经度 - &gt; Y / X转换算法