当用户滚动 MKMapView 时从 Core Data 加载其他对象
使用 iPhone 3.0 SDK 中的 MapKit,您可以创建符合 MKAnnotation 协议的对象。将它们加载到 MKMapView 上非常容易。但是,当用户滚动 MKMapView 时,就需要加载新注释。请求新对象的可能位置是 mapView:regionDidChangeAnimated: ,当地图区域更改时会调用该对象,然后用新注释添加/替换注释。
具体来说,我想查询核心数据以检索当前 MKCooperativeRegion (mapView.region) 中存在的所有对象,以便仅加载将在屏幕上显示的对象。 Core Data 中的对象具有纬度和经度属性(并且 CLLocation 属性在类 .m/.h 中定义,我可以从中手动填充)并将其用于 NSPredicate 来查找附近的对象。
由于核心数据数据库中存在多少对象的性质,我们无法将所有对象预加载为注释,否则我们将耗尽内存(并且速度会非常慢)。
如何仅检索在当前地图视图范围内具有位置的对象?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,英语不是我的母语。
一旦用户向下或向上滚动,区域值就会发生变化,特别是 regionDidChangeAnimated 中的 latitudeDelta 和 longitudeDelta。从那里,您可以获得当前mapView的边界..(minlat,minlong,maxlat,maxlong)
由于您已经计算出边界,所以您可以处理需要显示的对象,然后在地图中添加注释。我已经尝试过以下值..
请参见此处的图片.. http://img43。 imageshack.us/img43/703/picture1txe.png
从那里..我可以执行 sqlite3 sql 语句来获取该范围内的点..
SELECT * FROM pois WHERE categ_id = %@ AND (sat_latitude > %f AND sat_latitude < %f) AND (sat_longitude > %f AND sat_longitude < %f)
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)
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)