确定Point是否在半径范围内

时间:2012-01-31 08:32:57

标签: iphone objective-c

确定点X是否在点Y的100米半径内的最佳方法是什么?

CLLocation上有方法吗?

由于

2 个答案:

答案 0 :(得分:2)

- (CLLocationDistance) distanceFromLocation:(const CLLocation *)location

documentation

它计算距另一个CLLocation对象的距离。

答案 1 :(得分:0)

您可以使用此方法:

// proximity distance calculation
static const double kDegToRad = 0.017453292519943295769236907684886;
static const double kEarthRadiusM = 6372797.560856;

+ (double)distanceInMetersFromLoc:(CLLocation *)from toLoc:(CLLocation *)to
{
    return kEarthRadiusM * [self radianArcFrom:from.coordinate to:to.coordinate];
}

+ (double)radianArcFrom:(CLLocationCoordinate2D)from to:(CLLocationCoordinate2D)to
{
    double latitudeArc  = (from.latitude - to.latitude) * kDegToRad;
    double longitudeArc = (from.longitude - to.longitude) * kDegToRad;
    double latitudeHS = sin(latitudeArc * 0.5);
    latitudeHS *= latitudeHS;
    double lontitudeHS = sin(longitudeArc * 0.5);
    lontitudeHS *= lontitudeHS;
    double factor = cos(from.latitude * kDegToRad) * cos(to.latitude * kDegToRad);  
    return 2.0 * asin(sqrt(latitudeHS + factor * lontitudeHS));
}

将距离比较为

if([distanceInMetersFromLoc:location1 to:location2] < 100)
{
   // your condition is satisfied. you can write your code here
}