2014-03-24 82 views
0

我需要在阿姆斯特丹市中心周围显示20个注释。 我想将注释分为3类主题。我想用分段控制器控制这些主题注释。 示例:在段1中,我需要显示'x'注释。在段2中,我需要显示'x'(其他)注释。与分段3相同。每按一个分段时,我想删除其他分段并显示单击的分段。用于注释的分段控制器

这是我走到这一步:

的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 

更新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 
+2

在一个合适的解决方案所以,有什么问题吗? –

+0

对不起,我感到困惑。我的问题是,如何将这三个注释添加到我的分段控制器? – SwingerDinger

回答

1

下面是一个使用3个数组来保存注释的类别,选择基于分段控制器

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; 
    } 
} 
+0

谢谢,但我不明白使用3个动画数组,然后使用另一个外部数组的方式。这仍然是一个初学者。 – SwingerDinger

+0

创建4个NSMutableArrays - category1,category2,category3和locationArrays。将您的category1注释添加到category1,将我们的category2注释添加到category2数组中,并将其与category3相同。现在使用addObject将category1,category2和category3添加到locationArrays与您当前正在使用的相同,然后使用我的回答中的代码 – Paulw11

+0

感谢Paul,但它仍然不起作用。 用于添加类别ann。到我做的类别数组[地点addObject:category1];然后我将CategoriesArray放置在像这样的locationsArrays中:[locationArrays addObject:category1]; – SwingerDinger