【发布时间】:2014-03-17 17:53:23
【问题描述】:
我现在正在研究 mapView。这是场景:
- 所有注释都来自 JSON 对象(MySQL 表)。
- 注解按源表分组:
- 表 1-> Ofertas,表 2-> Cursos,表 3-> Eventos。
- 最初会显示一个地图区域,该区域足够大,可以稍后显示所有注释。
- 有一个 UISegmentedControll 可让用户选择应显示的注释组。
在此应用状态下,我需要进行以下更新:
- 每个注释组应具有不同的引脚颜色。
这是我用来根据所选索引将注释绘制到 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 方法以检测所选注释的组以更改引脚颜色。
【问题讨论】:
标签: ios mkmapview mkannotationview