Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does MKMetersBetweenMapPoints give me different results when I swap the parameters?

I'm actually trying to calculate the distance between the max and min point in the x and y coordinates for the MKMapPoints.

For that, I'm doing this (max distance in y axis):

MKMapPoint test1, test2;
double dist;
test1.x = 0.0;
test1.y = 0.0;
test2.x = 0.0;
test2.y = MKMapSizeWorld.height;
dist = MKMetersBetweenMapPoints(test2, test1);
NSLog(@"Distance %f",dist);

I get 18997878.291251 in the console. But when I change the distance calculation to:

dist = MKMetersBetweenMapPoints(test1, test2);

I get 18873651.664238, so I don't understand what's the difference. I don't even know if I'm doing the right thing to get the max values of distance in the x and y axes.

Any help will be appreciated.

like image 301
FranciscoAlexis Avatar asked Jan 11 '12 14:01

FranciscoAlexis


1 Answers

I guess it's an algorithm problem. Some kind of approximation that stops when a certain precision is achieved. That's why no commutation there. You can try sample code to get distance between two points on map without using MKMapPoints:

- (float) distanceToLPU {

        useDelegate

        CLLocationCoordinate2D pointACoordinate = appDelegate.usrLoc;
        CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];

        CLLocationCoordinate2D pointBCoordinate;
        pointBCoordinate.latitude = [self.latitude doubleValue];
        pointBCoordinate.longitude = [self.longitude doubleValue];

        CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude];  

        float distanceMeters = [pointALocation distanceFromLocation:pointBLocation];  

        [pointALocation release];
        [pointBLocation release];  

        NSString *dist = [NSString stringWithFormat:@" (%.0f м)", distanceMeters];
        NSLog(@"Distance to this LPU:%@", dist);

        return distanceMeters;
    }
like image 172
Denis Kutlubaev Avatar answered Nov 10 '22 18:11

Denis Kutlubaev