【问题标题】:Get all annotations around another annotation获取另一个注释周围的所有注释
【发布时间】:2012-07-30 13:37:00
【问题描述】:

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

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

但附近的Annotations 是空的。我尝试将经度与纬度交换,并为第三个和第四个参数设置更大的数字,但仍然没有结果。我该怎么做?

【问题讨论】:

    标签: objective-c ios mkannotation mkmaprect


    【解决方案1】:

    MKMapRect 使用与CLLocationDegrees 不同的MKMapPoint 单位

    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 将包含您正在搜索的注释(因为您使用它作为中心)。

    【讨论】:

    • 如果包含当前注解就没有问题,我知道怎么处理。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多