我已经使用这行代码在MkMapView
上添加了超过 2000 自定义注释。然后第一次收到内存警告和应用程序崩溃。
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
//MKAnnotationView *annotationView = nil;
else if ([annotation isKindOfClass:[MyAnnotation class]])
{
MKAnnotationView *annotationView = nil;
static NSString *identifier = @"identifier";
annotationView = (MKAnnotationView *)[mapViewForMapScreen dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView) {
annotationView.annotation = annotation;
} else {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.canShowCallout = YES;
}
MyAnnotation *anno=(MyAnnotation*)annotation;
tagForAnnotation=(int)anno.tag;
if (arrayForLatLong.count!=0)
{
if ([[[arrayForLatLong valueForKey:@"sourceType"] objectAtIndex:tagForAnnotation] isEqualToString:@"INSTAGRAM"])
{
annotationView.image=[UIImage imageNamed:@"instagram.png"];
annotationView.frame=CGRectMake(0, 0, 16, 22);
}
else if([[[arrayForLatLong valueForKey:@"sourceType"] objectAtIndex:tagForAnnotation] isEqualToString:@"TWITTER"])
{
annotationView.image=[UIImage imageNamed:@"twt_pin.png"];
annotationView.frame=CGRectMake(0, 0, 16, 22);
}
else if ([[[arrayForLatLong valueForKey:@"sourceType"] objectAtIndex:tagForAnnotation] isEqualToString:@"YOUTUBE"])
{
annotationView.image=[UIImage imageNamed:@"youtube.png"];
annotationView.frame=CGRectMake(0, 0, 16, 22);
}
else if ([[[arrayForLatLong valueForKey:@"sourceType"] objectAtIndex:tagForAnnotation] isEqualToString:@"MEETUP"])
{
annotationView.image=[UIImage imageNamed:@"meetup_iphone"];
annotationView.frame=CGRectMake(0, 0, 16, 22);
}
else if ([[[arrayForLatLong valueForKey:@"sourceType"] objectAtIndex:tagForAnnotation] isEqualToString:@"FLICKR"])
{
annotationView.image=[UIImage imageNamed:@"flikr.png"];
annotationView.frame=CGRectMake(0, 0, 16, 22);
}
else if ([[[arrayForLatLong valueForKey:@"sourceType"] objectAtIndex:tagForAnnotation] isEqualToString:@"VK"])
{
annotationView.image=[UIImage imageNamed:@"vk1"];
annotationView.frame=CGRectMake(0, 0, 16, 22);
}
}
infoButton=[[AsyncImageView alloc]init];
if ([[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"small"] isEqualToString:@""]){
if ([[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"medium"] isEqualToString:@""]){
if ([[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"large"] isEqualToString:@""]) {
[infoButton setImage:[UIImage imageNamed:@"profile"] forState:UIControlStateNormal];
}else{
[infoButton setImageURL:[NSURL URLWithString:[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"large"]]];
}
}else{
[infoButton setImageURL:[NSURL URLWithString:[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"medium"]]];
}
}else{
[infoButton setImageURL:[NSURL URLWithString:[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"]valueForKey:@"profilePic"] valueForKey:@"small"]]];
}
[infoButton setFrame:CGRectMake(20,10,30,30)];
infoButton.layer.cornerRadius=infoButton.frame.size.height/2.0;
infoButton.layer.masksToBounds = YES;
[infoButton addTarget:self action:@selector(actionForProfileImage:) forControlEvents:UIControlEventTouchUpInside];
infoButton.layer.borderWidth=1.0;
infoButton.layer.borderColor=[UIColor grayColor].CGColor;
infoButton.tag=tagForAnnotation;
//Followed Button:----
AsyncImageView *followedBtn=[[AsyncImageView alloc]init];
[followedBtn setFrame:CGRectMake(2,8,17,17)];
followedBtn.tag=tagForAnnotation;
if ([[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"isFollowed"] boolValue]==0 ){
[followedBtn setBackgroundImage:[UIImage imageNamed:@"star_grey.png"] forState:UIControlStateNormal];
}else{
[followedBtn setBackgroundImage:[UIImage imageNamed:@"star_blue.png"] forState:UIControlStateNormal];
}
[followedBtn addTarget:self action:@selector(actionForStarImage:) forControlEvents:UIControlEventTouchUpInside];
UIView*viewForCallOut=[[UIView alloc] initWithFrame:CGRectMake(0, 0,50, 50)];
[viewForCallOut addSubview:infoButton];
//[viewForCallOut addSubview:followedBtn];
annotationView.leftCalloutAccessoryView=viewForCallOut;
annotationView.canShowCallout = YES;
return annotationView;
}
return nil;
}
如果我有任何错误,请检查我的代码然后请告诉我。 提前致谢
答案 0 :(得分:0)
当您有大量数据点导致大量注释时,一种好方法是聚类数据点并为每个群集添加注释。随着地图缩放到群集中,您将表示群集的注释替换为群集中数据点的注释。缩小群集时,可以使用表示群集的注释替换表示数据点的注释。这种方法减少了注释的总数,使数据更容易理解。 fromValue和This SO question会为您提供有关群集的更多信息。