【发布时间】: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