GPS坐标的距离错误

时间:2014-09-26 13:39:38

标签: ios xcode gps cllocationdistance cllocationcoordinate2d

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);

这里是我正在使用的代码,它将从第一个位置为用户的新位置连续添加距离。请告诉我,我做错了什么,我得到distance米。结果总是像这样。

如果原始距离在坐标之间是10.97米,对我来说它只有1.097米 任何帮助都很明显

2 个答案:

答案 0 :(得分:0)

我正在使用CoreLocation方法。 例如,此方法将找到位置数组和单个位置之间的最短距离:

 + (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;
}

答案 1 :(得分:0)

这不会使semse m_1_pi = 1 / pi,

你将经度除以度/(180 * pi)

它应该是度* pi / 180或度* M_PI / 180.0会给你弧度