gpt4 book ai didi

iphone - MKAnnotation仅在区域内

转载 作者:行者123 更新时间:2023-12-03 20:45:44 26 4
gpt4 key购买 nike

如何只加载显示区域内的注释?目前我用这个方法加载所有注释

- (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];
}

然后我得到最大/最小纬度/经度并将其从核心数据中取出

- (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 数据库进行 sql 操作时,它实际上找到了它

SELECT * 
FROM caves
WHERE (
latitude > 43.930069
AND latitude < 50.169209
)
AND (
longitude > 3.078351
AND longitude < 10.207223
)

这是为什么?

最佳答案

我实际上找到了所有这些问题的解决方案。我跟踪该区域是否发生变化

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
[self reloadAnnotatons];
}

然后我得到最大/最小纬度/经度并将其从核心数据中取出

- (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);


NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Cave" inManagedObjectContext:self.context];
request.predicate = [NSPredicate predicateWithFormat:@"(latitude > %lf AND latitude < %lf) AND (longitude > %lf AND longitude < %lf)",minLat,maxLat,minLon,maxLon];


[self.mapView addAnnotations:[self.context executeFetchRequest:request error:NULL]];
}

关于iphone - MKAnnotation仅在区域内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7319083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com