2012-01-31 72 views
-1

確定點X在點Y的100米半徑內的最佳方法是什麼?確定點是否在半徑範圍內

CLLocation是否有方法?

由於

+1

計算距離兩個緯度/長之間以及與100比較,以便看看它是否小於100爲您案件。 – samfisher 2012-01-31 08:40:25

+0

CoreLocation框架包含一些方法,假設您正在處理緯度和經度座標。查看這篇文章瞭解更多信息:http://stackoverflow.com/questions/9029445/how-to-find-the-nearest-100-meters-latitude-and-longitude-depending-upon-the-cur/9031836# 9031836 – 2012-01-31 08:41:42

回答

2

看到

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

documentation

它計算從另一CLLocation物體的距離。

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 
} 
相關問題