iOS 核心数据查询地理空间数据集

标签 ios ios4 core-data

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

最佳答案

为了帮助您入门,这是我在我的 iOS 应用程序中使用的一种方法,它返回两个 CLLocationCoordinate2D 位置之间的距离(以米为单位),假设使用 Google 球形墨卡托投影(如果您想使用其他投影,您可以指定适当的展平比值(f)和半长轴值(a)。如果你想要坐标之间的前向和后向方位角值,你可以通过定义你自己的结构取消注释并返回 faz 和 baz 值以及距离。这个方法可以用于添加到每个“帐户”对象的距离以及 CLLocationManager 对象报告的当前位置,然后您可以根据距离轻松地对帐户对象数组进行排序和过滤。

基于位于此处的 Gerald 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);
}

关于iOS 核心数据查询地理空间数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6085218/

相关文章:

ios - 适用于IOS 4.3的Twitter集成

ios - 从nsbundle播放mp3

objective-c - 如何加密sqlite数据库核心数据?

objective-c - 核心数据 - 不同的值

ios - 如何在 Swift 中打印出核心数据保存错误

ios - 如何快速返回带有参数的先前 View Controller

ios - 单击 Collection View 单元格时振动 iPhone 无法正常工作 Swift

ios - 当主视图 segues 时,如何使 View 保持在屏幕上?

iOS TableView 加载相同的单元格

ios - 开发人员可以创建应用程序的最低 iOS 版本是多少?