How to know whether MKMapView visibleMapRect contains a Coordinate?

前端 未结 3 1962
情歌与酒
情歌与酒 2021-02-03 09:46

If I have a MKMapView and a CLLocationCoordinate2D how do you test whether the map\'s visible area contains the coordinate?

3条回答
  •  长发绾君心
    2021-02-03 10:44

    The fastest way is to use the inbuilt Apple functions which will make this sort of thing super quick!

    if(MKMapRectContainsPoint(mapView.visibleMapRect, MKMapPointForCoordinate(coordinate)))
    {
        //Do stuff
    }
    

    Where coordinate is your CLLocation2D.

    This will be much faster than working out coordinates with a bulk if statement. Reason is that Apple use a Quadtree and can do fast lookups for you.

提交回复
热议问题