Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding more annotations to UIMapView
    primarykey
    data
    text
    <p>I want to add more annotations to my mapview. I'm working with a class <code>Annotation</code> where there is a method <code>addAnnotation</code>. This method is being called in my <code>UIViewController</code> when I want to add a new annotation.</p> <p>But for some reason, only the annotation I added last, is displayed.</p> <p><strong>Annotation.h</strong></p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;MapKit/MKAnnotation.h&gt; @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 </code></pre> <p><strong>Annotation.m</strong></p> <pre><code>#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 </code></pre> <p><strong>Method in my UIViewController</strong></p> <pre><code>#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&lt;MKAnnotation&gt;)annotation { if ([annotation isKindOfClass:[MKUserLocation class]]) { //If annotation = user position ==&gt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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