0
double lat1 = coordinate1.latitude * M_1_PI/180;
double lat2 = coordinate2.latitude * M_1_PI/180;
double lon1 = coordinate1.longitude * M_1_PI/180;
double lon2 = coordinate2.longitude * M_1_PI/180;
double r = 6371;
double dlat = lat2 - lat1;
double dlon = lon2 - lon1;
double a = sin(dlat/2) * sin(dlat/2) + cos(lat1) * cos(lat2) * sin(dlon/2) * sin(dlon/2);
double c = 2 * atan2(sqrt(a), sqrt(1-a));
distance = distance + (r * c * 1000);

here it is the code i'm using and it will add the distance continously for the new location of user from his first location. pls tell me i'm doing anything wrong and i'm getting the distance in meters. the result is always coming like this.

if original distance is between the coordinates is 10.97 meters, for me it was only 1.097 meters Any help is appreciable

sreekanthk
  • 362
  • 2
  • 21

2 Answers2

0

I'm using CoreLocation methods. For example this method will find the shortest distance between array of locations and single location:

 + (NSInteger)distanceFrom:(NSArray*)locations toLocation:(CLLocation*)myLocation
{
    if(!locations || !locations.count) return -1;
    NSMutableArray *distance = [NSMutableArray new];
    for (CLLocation *loc in locations)
    {

        float latitudeB = loc.coordinate.latitude;
        float longitudeB = loc.coordinate.longitude;
        CLLocation *locB = [[CLLocation alloc] initWithLatitude:latitudeB  longitude:longitudeB];
        CLLocationDistance distanceBetweenAB = [myLocation distanceFromLocation:locB];
        [distance addObject:@(distanceBetweenAB)];

    }
    int distInt = [[distance valueForKeyPath:@"@min.intValue"] intValue];;
    return distInt;
}
Bogdan Matveev
  • 518
  • 2
  • 14
0

this doesn't make semse m_1_pi = 1/pi,

you are dividing longitude in degrees / (180 *pi)

it should be degrees *pi / 180 or degrees *M_PI / 180.0 will give you radians