1

マップビューにさらに注釈を追加したいと考えています。Annotationメソッドがあるクラスで作業していaddAnnotationます。このメソッドはUIViewController、新しい注釈を追加したいときに呼び出されています。

しかし、なぜか最後に追加したアノテーションだけが表示されます。

注釈.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: メソッドは必要ありません。1 つの MKAnnotation オブジェクトが場所を表します。

私の提案は、それに応じて Annotation クラスを変更し、それに準拠して、注釈を付けたい場所の数だけオブジェクトを作成することです。

ロケーション @property と -addAnnotation: メソッドを Annotation.h/m から削除

注釈.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 に答える