我需要在阿姆斯特丹市中心周围显示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
在一个合适的解决方案所以,有什么问题吗? –
对不起,我感到困惑。我的问题是,如何将这三个注释添加到我的分段控制器? – SwingerDinger