我需要在阿姆斯特丹市中心周围展示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
答案 0 :(得分: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;
}
}