Question

I'm building a application in whom one of the features will be shoving geographic coordinates on a custom UIImageView. I'm bad at math so I cant seem to get the right values. This is what I'm doing:

I have a image that is 2048x2048 and I put it in a UIScrollView and when I get coordinates, let's say "Sydney -33.856963, 151.215219" I turn them into a UIView coordinates (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];
}

The further I go from the center coordinates (0,0) the more the points are not accurate. If I have coordinates for a city in West Africa it will be right on spot, but Sydney is a lot off. How can I fix this?

Sydney image

Was it helpful?

Solution

I think the problem is that the earth is not flat. That means that you can not simple convert geo coordinates to 2-dimensional system of the view. http://en.wikipedia.org/wiki/Geographic_coordinate_system

Check this question and the correct answer: Converting longitude/latitude to X/Y coordinate

OTHER TIPS

Look at the white horizontal lines on the image you posted. They're not evenly spaced out - they get wider towards the bottom of the image. This means that your map image is not made using an Equirectangular projection, and is probably a Mercator projection image.

The code you have posted which converts lat/long to Y/X just by offset and scaling would only work for Equirectangular projection images.

For mercator projections, the conversion is more complex. Please see Covert latitude/longitude point to a pixels (x,y) on mercator projection.

So you have two options:

A) Use equirectangular projection map images

B) Continue using Mercator map images, and fix your lat/long -> Y/X conversion algorithm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top