【问题标题】:iOS MapKit show nearest annotations within certain distanceiOS MapKit 在一定距离内显示最近的注释
【发布时间】:2012-06-07 15:22:03
【问题描述】:

目前我正在为 iPhone/iPad 开发基于位置的应用程序。我的 MapKit 中有几个注释,我想做的是跟踪用户的位置并显示 3km 内的注释。有人可以给我一个开始吗?

【问题讨论】:

  • 您在哪个部分遇到了问题?追踪位置?寻找半径内的点?显示注释?
  • 显示来自数据库的注释并显示半径内的点。如果有的话,请给我推荐一些好的教程。

标签: ios5 mapkit xcode4.3 mkannotation


【解决方案1】:

抱歉,回复延迟了……这个问题刚刚从我的雷达上消失了。

我假设您有一个方法可以返回一组 NSValue 包装的 CLLocationCoordinate2D 结构(无论您的内部数据表示是什么,基本方法都是相同的)。然后,您可以使用类似于以下的方法过滤列表(警告:在浏览器中输入):

NSSet *locations = ...;
CLLocation centerLocation = ...; // Reference location for comparison, maybe from CLLocationManager
CLLocationDistance radius = 3000.; // Radius in meters
NSSet *nearbyLocations = [locations objectsPassingTest:^(id obj, BOOL *stop) {
        CLLocationCoordinate2D testCoordinate;
        [obj getValue:&testCoordinate];
        CLLocation *testLocation = [[CLLocation alloc] initWithLatitude:testCoordinate.latitude
                                                              longitude:testCoordinate.longitude];
        BOOL returnValue = ([centerLocation distanceFromLocation:testLocation] <= radius);
        [testLocation release];
        return returnValue;
    }
];

使用过滤后的坐标集,您可以创建MKAnnotation 实例并以通常的方式将它们添加到地图中,如Apple's documentation 中所述。

如果您有数千个测试位置,那么我认为这种方法可能会开始引发性能问题。然后,您需要切换使用点存储方法,例如 quadtrees,以减少需要精确过滤的点数。但不要过早优化!

希望有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    相关资源
    最近更新 更多