【问题标题】:Convert geographic coordinates to a CGPoint on a custon=m UIView将地理坐标转换为 custon=m UIView 上的 CGPoint
【发布时间】:2013-11-04 14:11:06
【问题描述】:

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

我有一个 2048x2048 的图像,我把它放在 UIScrollView 中,当我得到坐标时,假设“Sydney -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) 越远,点越不准确。如果我有西非某个城市的坐标,它就在现场,但悉尼离我很远。我怎样才能解决这个问题?

【问题讨论】:

  • 您的地图图像是什么投影?墨卡托?这很重要。
  • “悉尼离我们很远。” ——以什么方式? X,Y,两者都有?点的“偏离”与原点相比如何,它们离原点有多远?它只是看起来像一个扩展问题吗?
  • “coordY = (90.0f-Yl)*comparatorHeight”对我来说看起来很可疑。你确定它不应该是“coordY = (90.0f + Yl)*comparatorHeight”吗?
  • 我确定它是“coordY = (90.0f-Yl)*comparatorHeight”,Sydney 在 X 和 Y 两个方向上都会起飞我会在我的问题上放一张图片。你能解释一下关于图像投影的更多信息吗,我不知道那是什么
  • 我在回答中提到了两种常见的投影。

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


【解决方案1】:

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

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

【讨论】:

  • 是的,如果您使用的是 Equirectangular 投影,地理坐标可以直接转换为 2D。这就是为什么我问他是否使用墨卡托。
  • 感谢两位的建议。
【解决方案2】:

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

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

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

所以你有两个选择:

A) 使用等角投影地图图像

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

【讨论】:

    猜你喜欢
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    相关资源
    最近更新 更多