Objective c 删除注释(处理器较重)
此函数接受纬度/经度对数组。它将所有注释转换为Objective c 删除注释(处理器较重),objective-c,ios,cocoa-touch,mkmapview,mkannotation,Objective C,Ios,Cocoa Touch,Mkmapview,Mkannotation,此函数接受纬度/经度对数组。它将所有注释转换为MKAnnotations,然后对于地图上当前存在的每个注释,它会检查它是否存在于新的注释集中。如果存在,则保留注释,否则将删除注释 然后,对于每个新注释,它检查它是否当前在地图上;如果是,则保留它,否则删除它 这显然是非常密集的,我想知道是否有一个更快的方式做它 - (void)setAnnotationWithArray:(NSArray *)array { static BOOL processing = NO; if (pro
MKAnnotation
s,然后对于地图上当前存在的每个注释,它会检查它是否存在于新的注释集中。如果存在,则保留注释,否则将删除注释
然后,对于每个新注释,它检查它是否当前在地图上;如果是,则保留它,否则删除它
这显然是非常密集的,我想知道是否有一个更快的方式做它
- (void)setAnnotationWithArray:(NSArray *)array {
static BOOL processing = NO;
if (processing) {
return;
}
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
processing = YES;
NSMutableArray *annotationsArray = [NSMutableArray arrayWithCapacity:[array count]];
NSMutableArray *annotationsToRemove = [NSMutableArray array];
for (NSDictionary *dict in array) {
NSString *latStr = [dict objectForKey:@"Latitude"];
NSString *lonStr = [dict objectForKey:@"Longitude"];
NSString *title = [dict objectForKey:@"Location"];
double lat = [latStr doubleValue];
double lon = [lonStr doubleValue];
CLLocationCoordinate2D location;
location.latitude = lat;
location.longitude = lon;
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:title andCoordinate:location];
[annotationsArray addObject:newAnnotation];
[newAnnotation release];
}
for (id<MKAnnotation> oldAnnotation in [mv annotations]) {
CLLocationCoordinate2D oldCoordinate = [oldAnnotation coordinate];
BOOL exists = NO;
for (MapViewAnnotation *newAnnontation in annotationsArray) {
CLLocationCoordinate2D newCoordinate = [newAnnontation coordinate];
if ((newCoordinate.latitude == oldCoordinate.latitude)
&& (newCoordinate.longitude == oldCoordinate.longitude)) {
exists = YES;
break;
}
}
if (!exists) {
[annotationsToRemove addObject:oldAnnotation];
}
}
[annotationsArray removeObjectsInArray:[mv annotations]];
dispatch_async( dispatch_get_main_queue(), ^{
processing = NO;
[mv removeAnnotations:annotationsToRemove];
[mv addAnnotations:annotationsArray];
});
});
}
-(void)setAnnotationWithArray:(NSArray*)数组{
静态布尔处理=否;
如果(处理){
返回;
}
调度异步(调度获取全局队列(调度队列优先级默认为0)^{
处理=是;
NSMutableArray*annotationsArray=[NSMutableArray阵列容量:[阵列计数]];
NSMutableArray*annotationsToRemove=[NSMutableArray];
for(NSDictionary*数组中的dict){
NSString*latStr=[dict objectForKey:@“纬度”];
NSString*lonStr=[dict objectForKey:@“经度”];
NSString*title=[dict objectForKey:@“Location”];
双lat=[latStr doubleValue];
double lon=[lonStr doubleValue];
CLLOCATION坐标2D定位;
位置纬度=纬度;
location.longitude=lon;
MapViewAnnotation*newAnnotation=[[MapViewAnnotation alloc]initWithTitle:title和坐标:位置];
[annotationsArray addObject:newAnnotation];
[新注释发布];
}
用于(id为[mv annotations]中的oldAnnotation){
CLLocationCoordinate2D旧坐标=[oldAnnotation坐标];
BOOL存在=否;
对于(MapViewAnnotation*annotationsArray中的新注释){
CLLocationCoordinate2D新坐标=[newAnnontation坐标];
if((newCoordinate.latitude==oldcordinate.latitude)
&&(newCoordinate.longitude==oldcordinate.longitude)){
存在=是;
打破
}
}
如果(!存在){
[annotationsToRemove addObject:oldAnnotation];
}
}
[annotationsArray removeObjectsInArray:[mv annotations]];
dispatch\u async(dispatch\u get\u main\u queue()^{
处理=否;
[mv removeAnnotations:annotationsToRemove];
[mv addAnnotations:annotationsArray];
});
});
}
您可以使用removeObjectsInArray:(NSArray*)
例如:
NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray:[mapView annotations]];
[annotationsToRemove removeObjectsInArray:annotationsArray];
NSMutableArray *annotationsToAdd = [[NSMutableArray alloc] initWithArray:annotationsArray];
[annotationsToAdd removeObjectsInArray:[mapView annotations]];
它假设您的批注实现了hash和isEqual:但是应该更有效。您确定行
[annotationsArray removeObjectsInArray:[mv annotations]]代码>实际有效吗?您正在创建新的注释对象并将其放入annotationsArray中。这些新对象将不会“等同”于[mv annotations]
中的对象(即使属性值匹配),除非您实现了isEqual
,如Zoleas所说。如果该行不起任何作用,那么每次调用该方法时都会添加映射上已有注释的重复副本(可能导致性能降低)。