【问题标题】:segmented controller for annotations用于注释的分段控制器
【发布时间】:2014-03-24 21:37:26
【问题描述】:

我需要在阿姆斯特丹市中心周围展示大约 20 个注释。 我希望将注释分为 3 种主题。我想用分段控制器控制这些主题注释。 示例:在第 1 段中,我需要显示“x”注释。在第 2 段中,我需要显示“x”(其他)注释。与段 3 相同。每次按下其中一个段时,我都想删除其他段并显示我单击的段。

这是我目前得到的:

视图控制器:

#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

注解.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

【问题讨论】:

  • 那么问题是什么?
  • 很抱歉给您带来了困惑。我的问题是,如何将这三个注释添加到我的分段控制器?

标签: ios annotations nsmutablearray mapkit


【解决方案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;
    }
}

【讨论】:

  • 谢谢,但我不明白使用3个动画数组然后使用另一个外部数组的方式。在这方面仍然是初学者。
  • 创建 4 个 NSMutableArrays - category1、category2、category3 和 locationArrays。将您的 category1 注释添加到 category1,我的 category2 注释添加到 category2 数组,与 category3 相同。现在使用 addObject 将 category1、category2 和 category3 添加到 locationArrays,就像您当前所做的一样,然后使用我的答案中的代码
  • 感谢 Paul,但它仍然无法正常工作。用于添加类别 an。到我做的类别数组 [locations addObject:category1];然后我将 CategoriesArray 放在 locationArrays 中,如下所示: [locationArrays addObject:category1];
  • 尝试 [locations addObject:category1] 然后 [locationArrays addObject:locations] 这样您的 locationArrays 中的对象是包含您的注释的数组 - 这是假设 category1 是您的注释之一
  • 已经试过了。不知道问题似乎是什么。也许与我制作数组注释的方式有关:位置?
猜你喜欢
  • 1970-01-01
  • 2023-02-14
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
相关资源
最近更新 更多