检测选定的注释以更改引脚颜色
我现在正在做一个mapView。 这是场景:
- 所有注释都来自JSON对象(MySQL表)。
- 注释按源表分组:
- 表1-> Ofertas,表2-> Cursos,表3-> Eventos。
- 最初显示的地图区域足够大,以后可以显示所有的注释。
- 有一个UISegmentedControll让用户select应该显示哪个注释组。
在这个应用程序状态下,我将需要进行以下更新:
- 每个注释组应该具有不同的引脚颜色。
这是我正在使用的方法来绘制注释到mapView取决于所选的索引:
- (IBAction)changeOpcion:(id)sender{ if(miControl.selectedSegmentIndex == 0) { //[mapView_ clear]; [self removeAllPinsButUserLocation2]; NSLog(@"********0"); for ( int i=0;i<[categorias count];i++){ int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue]; if (grupo == 1){ double latitud = [[[categorias objectAtIndex:i] objectForKey:@"latitud"] doubleValue]; double longitud = [[[categorias objectAtIndex:i] objectForKey:@"longitud"]doubleValue]; CLLocationCoordinate2D lugar; lugar.latitude = latitud; lugar.longitude = longitud; NSString *nombre = [[categorias objectAtIndex:i] objectForKey:@"titulo"]; NSString *direccion = [[categorias objectAtIndex:i] objectForKey:@"direccion"]; CLLocationCoordinate2D coordinate3; coordinate3.latitude = latitud; coordinate3.longitude = longitud; myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre]; [self.mapView addAnnotation:annotation3]; } } } else if(miControl.selectedSegmentIndex == 1) { [self removeAllPinsButUserLocation2]; for ( int i=0;i<[categorias count];i++){ int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue]; if (grupo == 2){ double latitud = [[[categorias objectAtIndex:i] objectForKey:@"latitud"] doubleValue]; double longitud = [[[categorias objectAtIndex:i] objectForKey:@"longitud"]doubleValue]; CLLocationCoordinate2D lugar; lugar.latitude = latitud; lugar.longitude = longitud; NSString *nombre = [[categorias objectAtIndex:i] objectForKey:@"titulo"]; NSString *direccion = [[categorias objectAtIndex:i] objectForKey:@"direccion"]; CLLocationCoordinate2D coordinate3; coordinate3.latitude = latitud; coordinate3.longitude = longitud; myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre]; [self.mapView addAnnotation:annotation3]; } } //action for the second button } else if(miControl.selectedSegmentIndex == 2) { [self removeAllPinsButUserLocation2]; for ( int i=0;i<[categorias count];i++){ int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue]; if (grupo == 3){ double latitud = [[[categorias objectAtIndex:i] objectForKey:@"latitud"] doubleValue]; double longitud = [[[categorias objectAtIndex:i] objectForKey:@"longitud"]doubleValue]; CLLocationCoordinate2D lugar; lugar.latitude = latitud; lugar.longitude = longitud; NSString *nombre = [[categorias objectAtIndex:i] objectForKey:@"titulo"]; NSString *direccion = [[categorias objectAtIndex:i] objectForKey:@"direccion"]; CLLocationCoordinate2D coordinate3; coordinate3.latitude = latitud; coordinate3.longitude = longitud; myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre]; [self.mapView addAnnotation:annotation3]; } } } }
这是我为viewForAnnotation方法的代码:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation { if([annotation isKindOfClass:[MKUserLocation class]]) return nil; static NSString *identifier = @"myAnnotation"; MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; if (!annotationView) { annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; annotationView.pinColor = MKPinAnnotationColorPurple; annotationView.animatesDrop = YES; annotationView.canShowCallout = YES; }else { annotationView.annotation = annotation; } annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; return annotationView; }
我的第一个问题是:如何更新viewForAnnotation方法来检测所选注释的组以更改引脚颜色。
由于您一次只显示一个组(一种颜色)的注释,所以最简单,最直接(但粗糙的 – 不推荐)的方法是:
在viewForAnnotation
,根据pinColor
的当前值设置miControl.selectedSegmentIndex
。
如果您一次只显示一组注释,并且将分段控件(某些外部UI元素)的值与地图代理方法关联起来,那么这只能正常工作。 将来,您可能希望一次显示多个组,或者您可能想要更改控制显示组的UI,现在必须更改viewForAnnotation
等中的代码
注意,在viewForAnnotation
委托方法中,它传递一个对映射想要的视图( annotation
参数)的注解对象的引用。
如果注释对象本身知道它属于哪个组,则可以基于annotation
某些属性在viewForAnnotation
设置pinColor
。
所以推荐的方法是将注释对象本身所需的数据(添加到myAnnotation
类中)。 您可以添加grupo
属性,创build每个注释时设置此属性,然后在viewForAnnotation
检查它以设置pinColor
。 例如:
-
在myAnnotation.h中 :
@property (nonatomic, assign) int grupo;
-
在添加注释的
changeOpcion:
方法中:myAnnotation *annotation3 = [[myAnnotation alloc] initWith... annotation3.grupo = grupo; // <-- tell the annotation what group it's in [self.mapView addAnnotation:annotation3];
-
最后,在
viewForAnnotation
,在返回视图之前,根据grupo
设置pinColor
:if ([annotation isKindOfClass:[myAnnotation class]]) { myAnnotation *myAnn = (myAnnotation *)annotation; switch (myAnn.grupo) { case 1: annotationView.pinColor = MKPinAnnotationColorGreen; break; case 2: annotationView.pinColor = MKPinAnnotationColorPurple; break; default: annotationView.pinColor = MKPinAnnotationColorRed; break; } } return annotationView;
这样,您可以一次显示多个或所有组(颜色),并且viewForAnnotation
不依赖于外部UI。
只是对与您的问题无关的代码发表评论:
在changeOpcion:
方法中,通过注释循环的代码有不必要的重复。 if (grupo == 1)
, if (grupo == 2)
, if (grupo == 3)
)这三个块之间的唯一区别就是它正在检查的grupo
的值。
另一种方法是创build一个将grupo
作为参数的方法,然后从changeOpcion:
调用该方法。 例:
- (void)addAnnotationsForGroup:(int)selectedGrupo { [self removeAllPinsButUserLocation2]; NSLog(@"********0"); for (int i=0; i < [categorias count]; i++) { int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue]; if (grupo == selectedGrupo) { //code to create annotation here } } } - (IBAction)changeOpcion:(id)sender { switch (miControl.selectedSegmentIndex) { case 0: [self addAnnotationsForGroup:1]; break; case 1: [self addAnnotationsForGroup:2]; break; case 2: [self addAnnotationsForGroup:3]; break; default: break; } }