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

Objective c 将地理坐标转换为custon=m UIView上的CGPoint,objective-c,uiimageview,coordinate-systems,cgpoint,coordinate-transformation,Objective C,Uiimageview,Coordinate Systems,Cgpoint,Coordinate Transformation,我正在构建一个应用程序,其中一个功能将在自定义UIImageView上推送地理坐标。我数学不好,所以我似乎不能得到正确的值。这就是我正在做的: 我有一个2048x2048的图像,我把它放在UIScrollView中,当我得到坐标时,比如说“Sydney-33.856963151.215219”,我把它们转换成UIView坐标(x,y) -(void)viewDidLoad { [超级视图下载]; scrollView=[[UIScrollView alloc]initWithFrame:CGRe

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

我有一个2048x2048的图像,我把它放在
UIScrollView
中,当我得到坐标时,比如说“Sydney-33.856963151.215219”,我把它们转换成
UIView
坐标(x,y)

-(void)viewDidLoad
{
[超级视图下载];
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
scrollView.delegate=self;
scrollView.showsVerticalScrollIndicator=是;
scrollView.scrollEnabled=是;
scrollView.userInteractionEnabled=是;
[滚动视图设置盎司:否];
scrollView.minimumZoomScale=0.5;
scrollView.maximumZoomScale=100.0;
mainImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,图像大小,图像大小)];
mainImageView.image=[UIImage ImageName:@“map.jpg”];
mainImageView.contentMode=UIViewContentModeScaleAspectFit;
tipScroll.contentSize=CGSizeMake(图像大小,图像大小);
[滚动视图添加子视图:mainImageView];
[self.view addSubview:scrollView];
NSString*文件内容=@“-33.856963151.215219”;
NSArray*PointString=[fileContents组件由字符分隔集:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

对于(int i=0;i请查看您发布的图像上的白色水平线。它们的间距不均匀-它们向图像底部变宽。这意味着您的地图图像不是使用,可能是图像

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

对于墨卡托投影,转换更为复杂。请参阅

所以你有两个选择:

A) 使用等矩形投影贴图图像


B) 继续使用墨卡托地图图像,并修复你的lat/long->Y/X转换算法

我认为问题在于地球不是平坦的。这意味着你不能简单地将地理坐标转换为视图的二维系统

检查此问题和正确答案:

你的地图图像投影是什么?墨卡托?这很重要。“悉尼离得很远。”--以什么方式?X,Y,两者?点的“偏斜”与原点相比如何?它们离原点有多远?这看起来像是缩放问题吗?我觉得“坐标=(90.0f-Yl)*比较器高度”很可疑。你确定不应该这样吗“coordY=(90.0f+Yl)*比较器高度”?我肯定它是“coordY=(90.0f-Yl)*比较器高度”",Sydney在X和Y两个方向上爆炸了我会在我的问题上放一张图片。你能解释更多关于图像投影的内容吗,我不知道是什么。我在回答中提到了两种常见的投影。是的,如果你使用等矩形投影,地理坐标可以直接转换为2D。这就是为什么我问他是否使用了墨卡托投影。谢谢你们两位的建议。
- (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];
}