ios - segmented controller for annotations -


i need show 20 annotations around city center of amsterdam. want annotations separated in 3 kinds of subject. want control these subject annotations segmented controller. example: in segment 1 need show 'x' annotations. in segment 2 need show 'x' (other) annotations. same segment 3. each time press 1 of segments want remove others , show ones clicked.

this got far:

viewcontroller:

#import "viewcontroller.h" #import "annotations.h"  @interface viewcontroller ()  @end  @implementation viewcontroller @synthesize mymapview;  - (void)viewdidload { [super viewdidload];  //create 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 annotation of nes     break; case 1:     //for example:     //show here annotation of noordermarkt     break; case 2:     //for example:     //show here 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 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 , add other 'category 1' locations in 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 , add other 'category 2' locations in 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 , add other 'category 3' locations in same way     [self.locationarrays addobject:category3];      self.currentannotation = 0;      [self.mymapview addannotations:[locationarrays objectatindex:0]];  }  - (void)didreceivememorywarning {     [self setmymapview:nil];     [super didreceivememorywarning];     // dispose of resources can 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 

here solution uses 3 arrays hold categories of annotations , selects appropriate 1 based on 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]; // additional setup after loading view, typically nib.       self.locationarrays=[[nsmutablearray alloc]init];      //create 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 , add other 'category 1' locations in 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 , add other 'category 2' locations in 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 , add other 'category 3' locations in 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;     } } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -