2013-08-13 34 views

Trả lời

5

Làm thế nào về hai phương pháp này từ lớp MapKit:

1) Nhận hình chữ nhật có thể nhìn thấy bản đồ sử dụng:

@property(nonatomic, readonly) CGRect annotationVisibleRect 

http://developer.apple.com/library/ios/DOCUMENTATION/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instp/MKMapView/annotationVisibleRect

2) Sau đó được chú thích NSSet từ hình chữ nhật bản đồ tham số:

- (NSSet *)annotationsInMapRect:(MKMapRect)mapRect 

http://developer.apple.com/library/ios/DOCUMENTATION/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instm/MKMapView/annotationsInMapRect:

Vì vậy, tôi mong chờ một cái gì đó như thế này:

-(void)getAnotationsInVisibleMapRectangle 
{ 
    NSSet *annotationSet = [myMapView annotationsInMapRect:myMapView.annotationVisibleRect]; 

    // print number of annotations 
    NSLog(@"Number of annotations in rect: %d", annotationSet.count); 

    // this will return an array from the NSSet 
    NSArray *annotationArray = [annotationSet allObjects]; 
} 

Điều đó giúp đỡ?

+0

Đó là tốt đẹp, nhưng cần phải chuyển đổi CGRect (myMapView.annotationVisibleRect) để MKMapRect? "annotationInMapRect (MKMapRect)" ... – Nubaslon

+0

Cảm ơn! =) Nó giúp tôi) và tôi sử dụng "visibleMapRect" thay vì "annotationVisibleRect" – Nubaslon

+0

đã giúp tôi rất nhiều .. cảm ơn bạn –

9

Đối nhanh chóng 2.0, tôi thường sử dụng phần mở rộng MapView này để có được tất cả các chú thích hiện có thể nhìn thấy như là một mảng của MKAnnotations:

extension MKMapView { 
    func visibleAnnotations() -> [MKAnnotation] { 
     return self.annotationsInMapRect(self.visibleMapRect).map { obj -> MKAnnotation in return obj as! MKAnnotation } 
    } 
} 
+0

cực kỳ hữu ích. –

+0

nếu một số chú thích ở cạnh màn hình, nó không hoạt động – vinbhai4u

+0

Đối với trường hợp cụ thể đó, bạn có thể vượt qua một MapRect lớn hơn thay vì mặc định visibleMapRect. –

Các vấn đề liên quan