Question

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?

Was it helpful?

Solution

Sorry english is not my first language..

Once the user scrolls down or up.. the region values will change, particularly latitudeDelta and longitudeDelta from regionDidChangeAnimated. From there, you can get the bounds of your current mapView..(minlat, minlong, maxlat, maxlong)

minlat = current_coordinate_lat - latitudeDelta; //note everything is in dd (decimal degrees)

maxlat = current_coordinate_lat + latitudeDelta;

minlong = ...

maxlong = ...

Since you have the computed bounds already, then you can process which object needs to show, then add the annotation in the map. I have experimented with the values below..

See image here.. http://img43.imageshack.us/img43/703/picture1txe.png

From there.. i can do sqlite3 sql statement to get the points within that bounds..

SELECT * FROM pois WHERE categ_id = %@ AND (sat_latitude > %f AND sat_latitude < %f) AND (sat_longitude > %f AND sat_longitude < %f)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top