简体   繁体   English

MKA注释仅在区域内

[英]MKAnnotation only within region

How can I only load the annotations that are within the displayed region? 如何仅加载显示区域内的注释? At the moment I load all Annotation with this method 目前,我使用此方法加载所有注释

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

Any ideas or good documents on how I could remove all annotations that are outside the display and load the others? 关于如何删除显示屏之外的所有注释并加载其他注释的任何想法或好的文档?

Thanks a lot 非常感谢

EDIT: 编辑:

I have actually found a solution but it does not perfectly work somehow. 我实际上找到了一个解决方案,但是它不能以某种方式完美地工作。 I check if region has changed 我检查区域是否已更改

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

Then I get max/min lat/long and pull it out of coredata 然后我得到最大/最小纬度/经度并将其从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]];
}

Only problem is that it only finds the annotations if you zoom in a certain level which is strange. 唯一的问题是,如果您放大某个特定级别,它只会找到注释,这很奇怪。 For example 例如

When I zoomed out quite far it has a wide range from min to max. 当我缩小得很远时,它的范围从最小到最大。 Order is minLat,maxLat,minLong,MaxLong 顺序为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] (
)

As soon as I zooom in it does get the objects. 我刚进入动物园时,确实会得到物体。 Allthough it should already find it before. 尽管它应该早已找到它。

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>)",

It actually finds it when I sql directly the sqllite db with 当我直接使用sqllite db进行sql时,它实际上找到了它

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

Why is that? 这是为什么?

I actually find the solution to all of that. 我实际上找到了所有解决方案。 I track if the region has changed 我跟踪该地区是否发生了变化

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

Then I get max/min lat/long and pull it out of coredata 然后我得到最大/最小纬度/经度并将其从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);


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

As default MapView loads only the annotations that are within your display(ie, Onscreen). 默认情况下,MapView仅加载显示器内的注释(即,屏幕上)。 If you just move the screen you can reuse the Annotations with Reuse identifier for annotations. 如果仅移动屏幕,则可以将“带有重用标识符的注释”重用于注释。 So technically you dont need remove annotations that are out of your display. 因此,从技术上讲,您不需要删除不显示的注释。

Info from Apple doc 苹果文档中的信息

Because annotation views are needed only when they are onscreen, the MKMapView class provides a mechanism for queueing annotation views that are not in use. 由于注释视图仅在屏幕上时才需要,因此MKMapView类提供了一种对未使用的注释视图进行排队的机制。 Annotation views with a reuse identifier can be detached and queued internally by the map view when they move off screen. 具有重用标识符的注释视图可以在离开屏幕时由地图视图分离并在内部排队。 This feature improves memory use by keeping only a small number of annotation views in memory at once and by recycling the views you do have. 此功能通过一次在内存中仅保留少量批注视图并回收您拥有的视图来改善内存使用。 It also improves scrolling performance by alleviating the need to create new views while the map is scrolling. 通过减轻地图滚动时创建新视图的需要,它还提高了滚动性能。

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

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