views:

553

answers:

2

With MapKit in the iPhone 3.0 SDK, you create objects that conform to the MKAnnotation protocol. Loading these onto the MKMapView is very easy. However, when a user scrolls the MKMapView, it's time to load new annotations. A likely place to request the new objects would be in mapView:regionDidChangeAnimated: which is called when the map's region is changed, and then add/replace the annotations with the new ones.

Specifically, I'd like to query Core Data to retrieve all objects that exist within the current MKCoordinateRegion (mapView.region) so that I'm loading only the objects that will be shown on the screen. The objects in Core Data have latitude and longitude attributes (and a CLLocation attribute is defined in the class' .m/.h which I can populate manually from that) and use this for a NSPredicate for finding nearby objects.

Due to the nature of how many objects exist in the Core Data database, we can not preload ALL objects as annotations or else we'll run out of memory (and it would be excruciatingly slow).

How can I retrieve only the objects that have locations in the current mapview bounds?

+4  A: 
Rupert
This general concept worked well for me! The only thing that is annoying is that it's loading objects that are just beyond the bounds of the rectangle, so the user doesn't get to see it dropping the pins. That's mostly cosmetic, so it's not a rush to solve it.Any ideas why it's beyond the bounds? I can only assume the bounds are reporting larger that what is VISIBLE on the screen.Thoughts?
Kevin Elliott
A: 

Hi Kevin, Did you get a solution for this? I'm trying to use core data with mapkit in much the same way but am struggling. I'm ok using core data with table views but mapkit has thrown me. I too have lat and long within core data and would be very interested to learn how you populated you map view using your code data. Do you have an example or code snippet I could work through? if not, do you know have any links that may help? Much appreciated. Carl