Question

I need to show like 20 annotations around the city center of Amsterdam. I want the annotations be separated in 3 kinds of subject. I want to control these subject annotations with a Segmented controller. Example: in segment 1 I need to show 'x' annotations. in segment 2 I need to show 'x' (other) annotations. same with segment 3. Each time I press one of the segments I want to remove the others and show the ones I clicked.

This is what I got so far:

ViewController:

#import "ViewController.h"
#import "Annotations.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize myMapView;

- (void)viewDidLoad
{
[super viewDidLoad];

//Create the region
MKCoordinateRegion myRegion;

//Center
CLLocationCoordinate2D center;
center.latitude = 52.369331;
center.longitude = 4.893467;

//Span
MKCoordinateSpan span;
span.latitudeDelta = 0.04f;
span.longitudeDelta = 0.04f;

myRegion.center = center;
myRegion.span = span;

[myMapView setRegion:myRegion animated:YES];

//Annotation
NSMutableArray *locations = [[NSMutableArray alloc]init];
CLLocationCoordinate2D location;
Annotations *myAnn;

myAnn = [[Annotations alloc]init];
location.latitude = 52.369331;
location.longitude = 4.893467;
myAnn.coordinate = location;
myAnn.title = @"Nes";
myAnn.subtitle = @"Nes";
[locations addObject:myAnn];

myAnn = [[Annotations alloc]init];
location.latitude = 52.379680;
location.longitude = 4.886858;
myAnn.coordinate = location;
myAnn.title = @"Noordermarkt";
myAnn.subtitle = @"Noordermarkt";
[locations addObject:myAnn];

myAnn = [[Annotations alloc]init];
location.latitude = 52.371532;
location.longitude = 4.898080;
myAnn.coordinate = location;
myAnn.title = @"De Wallen";
myAnn.subtitle = @"De Wallen";
[locations addObject:myAnn];

[self.myMapView addAnnotations:locations]; 
} 

-(IBAction)setMap:(id)sender {

switch (((UISegmentedControl *) sender).selectedSegmentIndex ) {
    case 0:
    //for example:
    //Show here the annotation of Nes
    break;
case 1:
    //for example:
    //Show here the annotation of Noordermarkt
    break;
case 2:
    //for example:
    //Show here the annotation of De Wallen
    break;

default:
    break;

}
} 
@end

Annotations.H:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface Annotations : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@end

Update 2: ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController

@property (nonatomic, weak) IBOutlet MKMapView *myMapView;
@property (retain, nonatomic) NSMutableArray *locationArrays;
@property int currentAnnotation;

-(IBAction)setMap:(id)sender;

@end

ViewController.m

#import "ViewController.h"
#import "Annotations.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize myMapView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Create the region
    MKCoordinateRegion myRegion;

    //Center
    CLLocationCoordinate2D center;
    center.latitude = 52.369331;
    center.longitude = 4.893467;

    //Span
    MKCoordinateSpan span;
    span.latitudeDelta = 0.04f;
    span.longitudeDelta = 0.04f;

    myRegion.center = center;
    myRegion.span = span;

    [myMapView setRegion:myRegion animated:YES];

    //Annotation
    NSMutableArray *locations = [[NSMutableArray alloc]init];
    CLLocationCoordinate2D location;
    Annotations *myAnn;

    NSMutableArray *category1 = [[NSMutableArray alloc]init];
    NSMutableArray *category2 = [[NSMutableArray alloc]init];
    NSMutableArray *category3 = [[NSMutableArray alloc]init];
    NSMutableArray *locationArrays = [[NSMutableArray alloc]init];

    myAnn = [[Annotations alloc]init];
    location.latitude = 52.369331;
    location.longitude = 4.893467;
    myAnn.coordinate = location;
    myAnn.title = @"Nes";
    myAnn.subtitle = @"Nes";
    [category1 addObject:myAnn];
    //TODO create and add other 'category 1' locations in the same way
    [self.locationArrays addObject:category1];

    myAnn = [[Annotations alloc]init];
    location.latitude = 52.379680;
    location.longitude = 4.886858;
    myAnn.coordinate = location;
    myAnn.title = @"Noordermarkt";
    myAnn.subtitle = @"Noordermarkt";
    [category2 addObject:myAnn];
    //TODO create and add other 'category 2' locations in the same way
    [self.locationArrays addObject:category2];

    myAnn = [[Annotations alloc]init];
    location.latitude = 52.371532;
    location.longitude = 4.898080;
    myAnn.coordinate = location;
    myAnn.title = @"De Wallen";
    myAnn.subtitle = @"De Wallen";
    [category3 addObject:myAnn];

    myAnn = [[Annotations alloc]init];
    location.latitude = 52.368585;
    location.longitude = 4.886457;
    myAnn.coordinate = location;
    myAnn.title = @"Bijbels Museum";
    myAnn.subtitle = @"Bijbels Museum";
    [category3 addObject:myAnn];
    //TODO create and add other 'category 3' locations in the same way
    [self.locationArrays addObject:category3];

    self.currentAnnotation = 0;

    [self.myMapView addAnnotations:[locationArrays objectAtIndex:0]];

}

- (void)didReceiveMemoryWarning
{
    [self setMyMapView:nil];
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)setMap:(id)sender {
    int newAnnotations=((UISegmentedControl *) sender).selectedSegmentIndex;

    if (newAnnotations != self.currentAnnotation)
    {
        [self.myMapView removeAnnotations:[self.locationArrays objectAtIndex:self.currentAnnotation]];
        [self.myMapView addAnnotations:[self.locationArrays objectAtIndex:newAnnotations]];
        self.currentAnnotation = newAnnotations;
    }
}

@end
Was it helpful?

Solution

Here is a solution that uses 3 arrays to hold the categories of annotations and selects the appropriate one based on the segmented controller

ViewController.h 

@interface PWViewController : UIViewController

@property (strong,nonatomic) IBOutlet MKMapView *myMapView;
@property (strong,nonatomic) IBOutlet UISegmentedControl *mySegmentedControl;
@property int currentAnnotation;
@property (strong,nonatomic) NSMutableArray *locationArrays;

-(IBAction)setMap:(id)sender;


ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


    self.locationArrays=[[NSMutableArray alloc]init];

    //Create the region
    MKCoordinateRegion myRegion;

    //Center
    CLLocationCoordinate2D center;
    center.latitude = 52.369331;
    center.longitude = 4.893467;

    //Span
    MKCoordinateSpan span;
    span.latitudeDelta = 0.04f;
    span.longitudeDelta = 0.04f;

    myRegion.center = center;
    myRegion.span = span;

    [self.myMapView setRegion:myRegion animated:YES];

    //Annotation
    CLLocationCoordinate2D location;
    Annotation *myAnn;

    NSMutableArray *category1 = [[NSMutableArray alloc]init];
    NSMutableArray *category2 = [[NSMutableArray alloc]init];
    NSMutableArray *category3 = [[NSMutableArray alloc]init];

    myAnn = [[Annotation alloc]init];
    location.latitude = 52.369331;
    location.longitude = 4.893467;
    myAnn.coordinate = location;
    myAnn.title = @"Nes";
    myAnn.subtitle = @"Nes";
    [category1 addObject:myAnn];
    //TODO create and add other 'category 1' locations in the same way
    [self.locationArrays addObject:category1];

    myAnn = [[Annotation alloc]init];
    location.latitude = 52.379680;
    location.longitude = 4.886858;
    myAnn.coordinate = location;
    myAnn.title = @"Noordermarkt";
    myAnn.subtitle = @"Noordermarkt";
    [category2 addObject:myAnn];
    //TODO create and add other 'category 2' locations in the same way
    [self.locationArrays addObject:category2];

    myAnn = [[Annotation alloc]init];
    location.latitude = 52.371532;
    location.longitude = 4.898080;
    myAnn.coordinate = location;
    myAnn.title = @"De Wallen";
    myAnn.subtitle = @"De Wallen";
    [category3 addObject:myAnn];

    myAnn = [[Annotation alloc]init];
    location.latitude = 52.368585;
    location.longitude = 4.886457;
    myAnn.coordinate = location;
    myAnn.title = @"Bijbels Museum";
    myAnn.subtitle = @"Bijbels Museum";
    [category3 addObject:myAnn];
    //TODO create and add other 'category 3' locations in the same way
    [self.locationArrays addObject:category3];

    self.currentAnnotation = 0;

    [self.myMapView addAnnotations:[self.locationArrays objectAtIndex:0]];
}

-(IBAction)setMap:(id)sender {
    int newAnnotations=((UISegmentedControl *) sender).selectedSegmentIndex;

    if (newAnnotations != self.currentAnnotation)
    {
        [self.myMapView removeAnnotations:[self.locationArrays objectAtIndex:self.currentAnnotation]];
        [self.myMapView addAnnotations:[self.locationArrays objectAtIndex:newAnnotations]];
        self.currentAnnotation = newAnnotations;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top