How do I determine if a coordinate is in the currently visible map region?

后端 未结 3 1328
春和景丽
春和景丽 2020-12-15 14:20

I have a list of several hundred locations and only want to display an MKPinAnnotation for those locations currently on the screen. The screen starts with the user\'s curren

3条回答
  •  孤街浪徒
    2020-12-15 15:06

    In your code, you should pass a view for the toPointToView: option. I gave it my mapView. You have to specify an upper bound for the x and y too.

    Here's some code which worked for me (told me the currently visible annotations on my map, while looping through the annotation):

    for (Shop *shop in self.shops) {
        ShopAnnotation *ann = [ShopAnnotation annotationWithShop:shop];
        [self.mapView addAnnotation:ann];
    
        CGPoint annPoint = [self.mapView convertCoordinate:ann.coordinate 
                toPointToView:self.mapView];
    
        if (annPoint.x > 0.0 && annPoint.y > 0.0 && 
                annPoint.x < self.mapView.frame.size.width && 
                annPoint.y < self.mapView.frame.size.height) {
            NSLog(@"%@ Coordinate: %f %f", ann.title, annPoint.x, annPoint.y);
        }
    }
    

提交回复
热议问题