简体   繁体   English

当用户滚动MKMapView时,从Core Data中加载其他对象

[英]Load additional objects from Core Data when a user scrolls an MKMapView

With MapKit in the iPhone 3.0 SDK, you create objects that conform to the MKAnnotation protocol. 使用iPhone 3.0 SDK中的MapKit,您可以创建符合MKAnnotation协议的对象。 Loading these onto the MKMapView is very easy. 将它们加载到MKMapView上非常容易。 However, when a user scrolls the MKMapView, it's time to load new annotations. 但是,当用户滚动MKMapView时,该加载新的注释了。 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. 请求新对象的可能位置在mapView:regionDidChangeAnimated:中 ,当地图的区域发生更改时会调用该位置,然后用新的注释添加/替换注释。

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. 具体来说,我想查询核心数据以检索当前MKCoordinateRegion (mapView.region)中存在的所有对象,以便仅加载将在屏幕上显示的对象。 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. Core Data中的对象具有纬度和经度属性(并且在类'.m / .h中定义了CLLocation属性,可以从中手动填充该属性),并将其用于NSPredicate来查找附近的对象。

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). 由于Core Data数据库中存在多少个对象的性质,我们无法将所有对象预加载为批注,否则我们将耗尽内存(这会非常慢)。

How can I retrieve only the objects that have locations in the current mapview bounds? 如何仅检索在当前mapview边界中具有位置的对象?

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 . 一旦用户向下或向上滚动,区域值将更改,特别是来自regionDidChangeAnimated的 latitudeDelta和longitudeDelta。 From there, you can get the bounds of your current mapView..(minlat, minlong, maxlat, maxlong) 从那里,您可以获取当前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 在此处查看图片。http://img43.imageshack.us/img43/703/picture1txe.png

From there.. i can do sqlite3 sql statement to get the points within that bounds.. 从那里..我可以执行sqlite3 sql语句以获取该范围内的点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM