2011-05-22 94 views
1

我正在使用核心数据框架来管理一组帐户,其中还包括每个帐户的地理空间(GPS)坐标数据。如何根据设备的位置查询此数据以获取x英尺范围内的账户列表并按距离顺序列出它们?iOS核心数据查询地理空间数据集

回答

0

为了让你开始,我的iOS应用程序中使用了一个方法,它返回两个CLLocationCoordinate2D位置之间的距离(以米为单位),假设使用Google Spherical Mercator Projection(如果要使用另一个投影,可以指定合适的展平比例值(f)和半长轴值(a)。如果你想要坐标之间的前向和后向方位角值,可以通过定义你自己的结构来取消注释并返回faz和baz值以及距离。可以用于将距离添加到您的每个“帐户”对象以及您的CLLocationManager对象报告的当前位置,然后您可以根据它们的距离轻松地对帐户对象数组进行排序和过滤。

基于代码由G erald Evenden位于:http://article.gmane.org/gmane.comp.gis.proj-4.devel/3478

#define PI 3.141592653589793238462643 
#define EPS 5e-14 
#define DEG_TO_RAD 0.0174532925199432958 

// returns the geodesic distance in meters between two coordinates based on the google spherical mercator projection. 
- (int) geodesicDistanceFromCoordinate: (CLLocationCoordinate2D) fromCoord toCoordinate: (CLLocationCoordinate2D) toCoord { 
    double c, d, e, r, x, y, sa, cx, cy, cz, sx, sy, c2a, cu1, cu2, su1, tu1, tu2, ts, phi1, lam1, phi2, lam2, f, baz, faz, s, a; 

    phi1 = fromCoord.latitude * DEG_TO_RAD; 
    lam1 = fromCoord.longitude * DEG_TO_RAD; 
    phi2 = toCoord.latitude * DEG_TO_RAD; 
    lam2 = toCoord.longitude * DEG_TO_RAD; 
    f = 0; //google's spherical mercator projection has no flattening 
    a = 6378137; //earth's axis in meters used in google's projection 

    r = 1. - f; 
    tu1 = r * tan(phi1); 
    tu2 = r * tan(phi2); 
    cu1 = 1./sqrt(tu1 * tu1 + 1.); 
    su1 = cu1 * tu1; 
    cu2 = 1./sqrt(tu2 * tu2 + 1.); 
    ts = cu1 * cu2; 
    baz = ts * tu2; 
    faz = baz * tu1; 
    x = lam2 - lam1; 

    do { 
     sx = sin(x); 
     cx = cos(x); 
     tu1 = cu2 * sx; 
     tu2 = baz - su1 * cu2 * cx; 
     sy = sqrt(tu1 * tu1 + tu2 * tu2); 
     cy = ts * cx + faz; 
     y = atan2(sy, cy); 
     sa = ts * sx/sy; 
     c2a = -sa * sa + 1.; 
     cz = faz + faz; 
     if (c2a > 0.) 
      cz = -cz/c2a + cy; 
     e = cz * cz * 2. - 1.; 
     c = ((c2a * -3. + 4.) * f + 4.) * c2a * f/16.; 
     d = x; 
     x = ((e * cy * c + cz) * sy * c + y) * sa; 
     x = (1. - c) * x * f + lam2 - lam1; 
    } while (fabs(d - x) > EPS); 

//forward azimuth faz = atan2(tu1, tu2); 
//backward azimuth baz = atan2(cu1 * sx, baz * cx - su1 * cu2) + PI; 
    x = sqrt((1./r/r - 1.) * c2a + 1.) + 1.; 
    x = (x - 2.)/x; 
    c = (x * x/4. + 1.)/(1. - x); 
    d = (x * .375 * x - 1.) * x; 
    s = ((((sy * sy * 4. - 3.) * (1. - e - e) * cz * d/6. - e * cy) * d/4. + cz) * sy * d + y) * c * r; 
    return (int)(s * a); 
} 
+0

什么是度量单位?米? – VinnyD 2011-05-26 04:41:40

+0

是的,米。还有一个CLLocation对象的distanceFromLocation:方法,可用于查找位置与另一个位置之间的距离。 – pistachionut 2011-05-28 00:09:49

+0

MapKit是否使用与Google地图相同的投影,或者是否存在已知的展平比例? – 2013-12-03 16:56:20