我正在构建一个应用程序,其中的一个功能是在自定义的< code>UIImageView上推送地理坐标。我不擅长数学,所以我似乎不能得到正确的数值。这就是我正在做的:
我有一个2048x2048的图像,我把它放在一个< code>UIScrollView中,当我得到坐标时,比如说“Sydney -33.856963,151.215219”,我把它们转换成一个< code>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)越远,点越不准确。如果我有西非某个城市的坐标,它就在现场,但悉尼离我很远。我怎么解决这个问题?
我认为问题在于地球不是平的。这意味着你不能简单地将地理坐标转换成二维系统的视图。http://en.wikipedia.org/wiki/Geographic_coordinate_system
检查此问题和正确答案:将经度/纬度转换为X/Y坐标
看看你张贴的图片上的白色水平线。它们的间距不均匀——它们朝着图像的底部变宽。这意味着地图图像不是使用等角投影生成的,可能是墨卡托投影图像。
您发布的仅通过偏移和缩放将纬度/经度转换为Y/X的代码仅适用于等角投影图像。
对于墨卡托投影,转换更为复杂。请参见墨卡托投影上一个像素(x,y)的Covert纬度/经度点。
所以你有两个选择:
A) 使用等距柱状投影地图图像
B) 继续使用墨卡托地图图像,并修复经度/经度 -