1

我想在我的地图视图中添加更多注释。我正在使用一个Annotation有方法的类addAnnotationUIViewController当我想添加新注释时,我会调用此方法。

但由于某种原因,只显示我最后添加的注释。

注释.h

#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>

@interface Annotation : NSObject {
CLLocationCoordinate2D cooridnate;
NSString *title;
NSString *subtitle;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@property (nonatomic, retain) NSMutableArray *locations;

-(void)addAnnotation: (NSString *)initTitle : (NSString *)initSubtitle : (CLLocationCoordinate2D) initCoordinate;

@end

注释.m

#import "Annotation.h"

@implementation Annotation

@synthesize coordinate, title, subtitle, locations;

-(void)addAnnotation: (NSString *)initTitle : (NSString *)initSubtitle : (CLLocationCoordinate2D) initCoordinate {

locations = [[NSMutableArray alloc] init];

self.coordinate = initCoordinate;
self.title = [NSString stringWithFormat:@"%@", initTitle];
self.subtitle = [NSString stringWithFormat:@"%@", initSubtitle];

[locations addObject:self]; // add all my anotations with location to an array

}

@end

我的 UIViewController 中的方法

#import "MapViewController.h"

@interface MapViewController ()

@end

@implementation MapViewController

@synthesize mapView;

#pragma mark - ViewController methods

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

[self initMapView];
ann = [[Annotation alloc] init]; // making place in memory
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - UIMapController methods

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
[self createAnnotations];
}

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

if ([annotation isKindOfClass:[MKUserLocation class]]) {
    //If annotation = user position ==> DON'T CHANGE
    return nil;
}

MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
MyPin.pinColor = MKPinAnnotationColorRed;

UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:@selector(showInfo:) forControlEvents:UIControlEventTouchUpInside];

MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = NO;
MyPin.animatesDrop= FALSE;
MyPin.canShowCallout = YES;

return MyPin;
}

#pragma mark - MY Methods

-(void) initMapView {
[mapView setMapType:MKMapTypeStandard]; // standaard maptype
[mapView setScrollEnabled:YES];
[mapView setZoomEnabled:YES];
[mapView setDelegate:self]; // gegevens van de map terugsturen naar deze viewController en de gebruiker
[mapView setShowsUserLocation:YES];
[self focusWorld];
}

-(void) createAnnotations {
[self initSomeExamples];
[self.mapView addAnnotations: ann.locations];

}

-(void) focusWorld {
MKCoordinateRegion worldRegion = MKCoordinateRegionForMapRect(MKMapRectWorld); // regio instellen op World
mapView.region = worldRegion; // regio doorgeven naar onze mapView
}

-(void) initSomeExamples {

// Washington
l1.latitude = 38.892102;
l1.longitude = -77.029953;

// Antwerp
l2.latitude = 51.219787;
l2.longitude = 4.411011;

// Egypt
l3.latitude = 29.993002;
l3.longitude = 31.157227;

// Brazil
l4.latitude = -22.900846;
l4.longitude = -43.212662;

[ann addAnnotation:@"America has a new President !" :@"Washington DC" :l1]; // call method to add an annotation with these parameters
[ann addAnnotation:@"Revolutionary app by Belgium student" :@"Antwerp" :l2];
[ann addAnnotation:@"The new Arabic revolution" :@"Egypt, Tahir square" :l3];
[ann addAnnotation:@"World Championchip football" :@"Rio de Janeiro" :l4];

}


#pragma mark - ACTIONS

-(void)showInfo:(id)sender {
NSLog(@"Show info");
}

@end
4

1 回答 1

1

您不需要位置 @property 和 - addAnnotation: 方法。一个 MKAnnotation 对象代表一个位置。

我的建议是相应地修改 Annotation 类,以及符合并创建与您想要注释的位置数量一样多的对象。

从 Annotation.h/m 中删除位置 @property 和 -addAnnotation: 方法

注释.h

#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>

@interface Annotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

-(id) initWithTitle: (NSString *)initTitle : (NSString *)initSubtitle : (CLLocationCoordinate2D) initCoordinate; 

@end

注释.m

#import "Annotation.h"

@implementation Annotation

@synthesize coordinate, title, subtitle;

-(id) initWithTitle: (NSString *)initTitle : (NSString *)initSubtitle : (CLLocationCoordinate2D) initCoordinate {
    self = [super init];
    if ( self ) {
        self.coordinate = initCoordinate;
        self.title = [NSString stringWithFormat:@"%@", initTitle];
        self.subtitle = [NSString stringWithFormat:@"%@", initSubtitle];
    }
    return self; // correction appreciated.
}

@end

视图控制器中的一些方法发生了变化

-(void) initSomeExamples {

// Washington
l1.latitude = 38.892102;
l1.longitude = -77.029953;

// Antwerp
l2.latitude = 51.219787;
l2.longitude = 4.411011;

// Egypt
l3.latitude = 29.993002;
l3.longitude = 31.157227;

// Brazil
l4.latitude = -22.900846;
l4.longitude = -43.212662;

NSArray *annotations = @[
[[Annotation alloc] initWithTitle:@"America has a new President !" :@"Washington DC" :l1],
[[Annotation alloc] initWithTitle:@"Revolutionary app by Belgium student" :@"Antwerp" :l2],
[[Annotation alloc] initWithTitle:@"The new Arabic revolution" :@"Egypt, Tahir square" :l3],
[[Annotation alloc] initWithTitle:@"World Championchip football" :@"Rio de Janeiro" :l4] ];

// Adding 4 annotation objects
[self.mapView addAnnotations:annotations];

}

-(void) createAnnotations {
    [self initSomeExamples];
    // Annotations are added in -initSomeExamples method.
}
于 2012-10-18T14:19:50.687 回答