iOS MapKit在一定距离内显示最近的注释 [英] iOS MapKit show nearest annotations within certain distance

查看:124
本文介绍了iOS MapKit在一定距离内显示最近的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

对于延迟回复感到抱歉......问题刚刚落到我的视线中。



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

  NSSet * locations = ...; 
CLLocation centerLocation = ...; //用于比较的参考位置,可能来自CLLocationManager
CLLocationDistance radius = 3000。; //以米为单位的半径
NSSet * nearbyLocations = [locations objectsPassingTest:^(id obj,BOOL * stop){
CLLocationCoordinate2D testCoordinate;
[obj getValue:& testCoordinate];
CLLocation * testLocation = [[CLLocation alloc] initWithLatitude:testCoordinate.latitude
经度:testCoordinate.longitude];
BOOL returnValue =([centerLocation distanceFromLocation:testLocation]< = radius);
[testLocation release];
返回returnValue;
}
];

使用过滤后的坐标集,您可以创建 MKAnnotation 实例并按常规方式将它们添加到地图中,如 Apple的文档



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



希望有所帮助!


Currently i am working on a Location based application for iPhone/iPad . I have several annotations in my MapKit , what i want to do is to track the location of the user and shows the annotations that are within the 3km . Can somebody give me a start ?

解决方案

Sorry for the delayed response... the question just fell off my radar.

I'm going to suppose that you have a method that returns a set of NSValue-wrapped CLLocationCoordinate2D structs (the basic approach is the same regardless of what your internal data representations are). You can then filter the list using a method something akin to the following (warning: typed in browser):

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;
    }
];

With the filtered set of coordinates in hand, you can create MKAnnotation instances and add them to the map in the usual manner, as described in Apple's documentation.

If you have many thousands of test locations then I suppose this approach could start to incur performance issues. You would then want to switch your point storage approach to use, e.g., quadtrees, to reduce the number of points that need to be precision-filtered. But don't optimize prematurely!

Hope that helps!

这篇关于iOS MapKit在一定距离内显示最近的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆