objective-ciphonecocos2d-iphoneuiaccelerometer

finding the distance from point A to point B for iPhone


Hey I'm trying to calculate the distance of 1 spot to another on the iPhone (horizontally) Im not sure how


Solution

  • Hi you need to create to instance of CLLocation, and call the methods to calculate.

    CLLocation *locationOne = [[CLLocation alloc] initWithLatitude:[point coordinate].latitude longitude:[point coordinate].longitude];
    CLLocation *locationTwo = [[CLLocation alloc] initWithLatitude:[secondPoint coordinate].latitude longitude:[secondPoint coordinate].longitude];
    double distanceInMeters = [locationOne distanceFromLocation:locationTwo];
    

    If you want distances between two points, you should remember Pitagora:

    CGPoint pointA, pointB;
    double distancesInPoints = sqrt((pointA.x-pointB.x)*(pointA.x-pointB.x) + (pointA.y-pointB.y)*(pointA.y-pointB.y));