11

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
  • 1
    Related: http://stackoverflow.com/questions/5558854/order-of-cllocation-objects-in-distancefromlocation –  Jan 11 '12 at 14:55
  • should the log line be NSLog(@"Distance %f",dist); – Damo Jan 11 '12 at 15:36
  • sorry, is a typo. The variable name is dist. (Corrected) – FranciscoAlexis Jan 11 '12 at 18:24
  • 3
    I believe the proper way of using `MKMetersBetweenMapPoints` is by converting `CLLocationCoordinate2D` structures to `MKMapPoint` structures by using `MKMapPointForCoordinate` and then use `MKMetersBetweenMapPoints` to get the distance in meters. This would probably avoid any internal range errors. – Krishna K Jan 17 '12 at 18:16
  • What happens if you set test2.y to MKMapSizeWorld.height - 1 ? – erikprice Jan 21 '12 at 23:51

1 Answers1

4

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;
    }
Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70