objective-c - 获取另一个注解周围的所有注解

标签 objective-c ios mkannotation mkmaprect

我正在尝试获取指定区域上另一个注释周围的所有注释,但我不知道该怎么做。现在我正在尝试:

MKMapRect mapRect = MKMapRectMake(annotation.coordinate.longitude, annotation.coordinate.latitude, 10.0, 10.0);
NSSet *nearbyAnnotations = [map annotationsInMapRect:mapRect];

但是 nearbyAnnotations 是空的。我尝试通过交换经度和纬度以及第三和第四参数的更大数字,但仍然没有结果。我应该怎么做?

最佳答案

MKMapRect 使用 MKMapPoint 单位,CLLocationDegrees 相同。

MKMapRectMake function需要左上角的 MKMapPoint,然后是宽度和高度(同样以 MKMapPoint 为单位)。

基本上,您需要使用 MKMapPointForCoordinate 函数来帮助您完成从度数到 MKMapPoint 单位的转换。

首先,您可以构造一个MKCoordinateRegion,然后将其转换为一个MKMapRect

例如:

//create a region 10km around the annotation...
MKCoordinateRegion mapRegion = MKCoordinateRegionMakeWithDistance
                                 (annotation.coordinate, 10000, 10000);

//convert the MKCoordinateRegion to an MKMapRect...
MKMapRect mapRect = [self mapRectForCoordinateRegion:mapRegion];

mapRectForCoordinateRegion 方法是您必须编写的内容。

有关一种编写方式的示例,请参阅此答案:
How to make the union between two MKCoordinateRegion


顺便说一句,请注意,在您的情况下,annotationsInMapRect 将包含您正在搜索的注释(因为您将其用作中心)。

关于objective-c - 获取另一个注解周围的所有注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11722716/

相关文章:

ios - 奇怪的错误 - 对象变为零

ios - jsonmodel - 模型级联(模型包括其他模型)

ios - UICollectionViewCell 是否有 viewWillAppear 或我可以将数据传递给的东西

ios - 如何创建具有不同纹理的 SCNNode 的新实例

javascript - 在 iPad 中隐藏地址栏

ios - 如何在 Swift 中选择用户的位置注释

swift - 如何在 MKAnnotationView 的标注中自定义标题/副标题字体或只是隐藏它们?

iphone - MKAnnotationView 不可拖动,尽管我认为它已正确实现?

c++ - 如何在 Windows 和 Mac 上提供原生用户界面?

ios - UIImageView 高度不是自动布局约束后子图像高度的高度。