I am now working on a mapView. This is the scenario:
- All annotations are coming from JSON objects (MySQL tables).
- Annotations are grouped by source table:
- Table 1-> Ofertas, Table 2-> Cursos, Table 3-> Eventos.
- Initially there is shown a map region which is big enough to show later all annotations.
- There is a UISegmentedControll to let the user select which annotations group should be shown.
At this app state, I will need to make the following updates:
- Each annotation group should have a different pin colour.
This is the method I am using to draw the annotations to the mapView depending on the selected index:
- (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];
}
}
}
}
And this is my code for the viewForAnnotation method:
- (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;
}
My first question is: how to update the viewForAnnotation method to detect the group of the selected annotation to change the pin colour.