gpt4 book ai didi

iphone - 可以在MKMapView上绘制的最大注释数(MKAnnotation)?

转载 作者:可可西里 更新时间:2023-11-01 04:48:21 27 4
gpt4 key购买 nike

我想在 map View 上添加一些注释(大约 500),但它似乎显示的最大值是 100。如果我超过 100,则不会调用 viewForAnnotation 委托(delegate)方法。但是,它非常适合 100 以下的注释。

代码如下:(仅当 annotationArray 数组的计数小于 101 时有效)

_allPoints = [[NSMutableArray alloc] init];

NSString* responseFile = [[NSBundle mainBundle] pathForResource:@"Datafile" ofType:@"txt"];
NSData *sampleData = [NSData dataWithContentsOfFile:responseFile];
if (sampleData) {
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:sampleData

options:kNilOptions
error:&error];

NSArray* response = [json objectForKey:@"response"];
for (NSDictionary *lineDict in response) {
if ([[lineDict objectForKey:@"data"] isKindOfClass:[NSArray class]]) {
SinglePoint *singlePoint = [[SinglePoint alloc] initWithDictionary:lineDict];
[_allPoints addObject:singlePoint];
}
else {
NSLog(@"Error");
}
}
}


_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[_mapView setDelegate:self];
[self.view addSubview:_mapView];

NSMutableArray *annotationArray = [[NSMutableArray alloc] init];
for (int i=0; i<[_allPoints count]; i++) {
SinglePoint *singlePoint = [_allPoints objectAtIndex:i];
NVPolylineAnnotation *annotation = [[NVPolylineAnnotation alloc] initWithPoint:singlePoint mapView:_mapView];
[annotationArray addObject:annotation];
}
[_mapView addAnnotations:(NSArray *)annotationArray];

CLLocationCoordinate2D centerCoord = { 28.632747, 77.219982 };
[_mapView setCenterCoordinate:centerCoord zoomLevel:12 animated:NO];

委托(delegate)方法是:

编辑:根据评论,开始重用 View 但没有运气 :(

if ([annotation isKindOfClass:[NVPolylineAnnotation class]]) {
static NSString *viewIdentifier = @"annotationView";
NVPolylineAnnotationView *annotationView = (NVPolylineAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:viewIdentifier];
if (annotationView == nil) {
annotationView = [[NVPolylineAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:viewIdentifier mapView:_mapView];
}
return annotationView;
}
return nil;

我在文档或其他任何地方都找不到任何限制。会不会是内存问题?

最佳答案

MKMapViewannotationViews 没有限制。但是在一定数量的 View (+1000) 之上,它会变得非常滞后且无法使用。

我相信,这是因为您完全错误地处理了 annotationView 管理。您不应该为每个注释创建唯一的 View ,即使您使用的是 ARC。您应该重用每个未使用的 View ,例如 UITableView 的单元格。

有一些很好的教程,比如 Introduction to MapKit on iOS - Ray Wenderlich .

如果这不能解决您的问题,您应该尝试调试您的注解类。 (NVPolylineAnnotationNVPolylineAnnotationView)。也许有什么不对劲。您是否还检查了点数组的坐标是否相等?

关于iphone - 可以在MKMapView上绘制的最大注释数(MKAnnotation)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10145976/

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