How do you check if an MKAnnotation is available within a MKCoordinateRegion

后端 未结 1 1145
粉色の甜心
粉色の甜心 2021-02-01 10:47

I\'ve noticed that if I use MKMapView\'s selectAnnotation:animated:, that it will scroll my map off screen if the MKAnnotation is not displayed in the current MKCoo

相关标签:
1条回答
  • 2021-02-01 11:10

    Use the annotationsInMapRect: method in the MKMapView class. It returns a NSSet of all annotation objects that are visible in the given map rect. Use the containsObject: method of NSSet to test if the annotation is present in that set of visible annotations.

    MKMapRect visibleMapRect = aMapView.visibleMapRect;
    NSSet *visibleAnnotations = [aMapView annotationsInMapRect:visibleMapRect];
    BOOL annotationIsVisible = [visibleAnnotations containsObject:someAnnotation];
    

    Also visibleMapRect is same as the region but just a different form of representation. Take from the docs,

    visibleMapRect

    The area currently displayed by the map view.

    @property(nonatomic) MKMapRect visibleMapRect

    This property represents the same basic information as the region property but specified as a map rectangle instead of a region.

    0 讨论(0)
提交回复
热议问题