Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't need locations @property and - addAnnotation: method. One MKAnnotation object represents a location.</p> <p>My suggestion is to modify Annotation class accordingly, as well as conforming and create it's objects as many as number of locations you would like to annotate.</p> <p>Remove locations @property and -addAnnotation: method from Annotation.h/m</p> <p><strong>Annotation.h</strong></p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;MapKit/MKAnnotation.h&gt; @interface Annotation : NSObject &lt;MKAnnotation&gt; { 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 </code></pre> <p><strong>Annotation.m</strong></p> <pre><code>#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 </code></pre> <p><strong>Some methods changed in your view controller</strong></p> <pre><code>-(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. } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload