【发布时间】:2011-09-07 18:41:29
【问题描述】:
如何只加载显示区域内的注释?目前我使用此方法加载所有注释
- (void)reloadAnnotatons
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Cave" inManagedObjectContext:self.context];
request.predicate = [NSPredicate predicateWithFormat:@"(latitude > 0) AND (longitude > 0)"];
[self.mapView addAnnotations:[self.context executeFetchRequest:request error:NULL]];
}
关于如何删除显示之外的所有注释并加载其他注释的任何想法或好的文档?
非常感谢
编辑:
我实际上已经找到了一个解决方案,但它不能以某种方式完美地工作。我检查区域是否已更改
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
[self reloadAnnotatons];
}
然后我得到 max/min lat/long 并将其从 coredata 中提取出来
- (void)reloadAnnotatons
{
double minLat = self.mapView.region.center.latitude - (self.mapView.region.span.latitudeDelta / 2);
double maxLat = self.mapView.region.center.latitude + (self.mapView.region.span.latitudeDelta / 2);
double minLon = self.mapView.region.center.longitude - (self.mapView.region.span.longitudeDelta / 2);
double maxLon = self.mapView.region.center.longitude + (self.mapView.region.span.longitudeDelta / 2);
NSLog(@"%f %f %f %f",minLat,maxLat,minLon,maxLon);
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Cave" inManagedObjectContext:self.context];
request.predicate = [NSPredicate predicateWithFormat:@"(latitude > %f AND latitude < %f) AND (longitude > %f AND longitude < %f)",minLat,maxLat,minLon,maxLon];
//NSLog(@"%@",[self.context executeFetchRequest:request error:NULL]);
[self.mapView addAnnotations:[self.context executeFetchRequest:request error:NULL]];
}
唯一的问题是它只有在你放大某个级别时才会找到注释,这很奇怪。例如
当我缩小很远时,它的范围很广,从最小值到最大值。顺序为 minLat,maxLat,minLong,MaxLong
2011-09-07 08:48:15.907 CaveConditions[9028:707] 43.930069 50.169209 3.078351 10.207223
2011-09-07 08:48:15.914 CaveConditions[9028:707] (
)
只要我放大它就会得到对象。尽管它之前应该已经找到它。
2011-09-07 08:48:35.363 CaveConditions[9028:707] 46.277977 48.327505 5.453421 7.806564
2011-09-07 08:48:35.376 CaveConditions[9028:707] (
"<Cave: 0x1d8710> (entity: Cave; id: 0x1cdd10 <x-coredata://738720B4-DCF4-40BA-BD83-A459CECE5F29/Cave/p2> ; data: <fault>)",
"<Cave: 0x1941c0> (entity: Cave; id: 0x1d1e70 <x-coredata://738720B4-DCF4-40BA-BD83-A459CECE5F29/Cave/p47> ; data: <fault>)",
"<Cave: 0x190200> (entity: Cave; id: 0x1e62b0 <x-coredata://738720B4-DCF4-40BA-BD83-A459CECE5F29/Cave/p57> ; data: <fault>)",
"<Cave: 0x1e4960> (entity: Cave; id: 0x1a1560 <x-coredata://738720B4-DCF4-40BA-BD83-A459CECE5F29/Cave/p67> ; data: <fault>)",
当我直接使用 sqllite db 时,它实际上找到了它
SELECT *
FROM caves
WHERE (
latitude > 43.930069
AND latitude < 50.169209
)
AND (
longitude > 3.078351
AND longitude < 10.207223
)
这是为什么呢?
【问题讨论】:
标签: iphone mkannotation region