5

I am adding multiple annotations programmatically like this:

- (void)addAnnotations{
    NSInteger i;
    CLLocationCoordinate2D location;

    for ( i = 0 ; i < [storeLatitude count] ; i ++ ){
        location.latitude = [[storeLatitude objectAtIndex:i] floatValue];
        location.longitude = [[storeLongitude objectAtIndex:i] floatValue];

        MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:[listOfStores objectAtIndex:i] andCoordinate:location];
        [self.mapView addAnnotation:newAnnotation];
        [newAnnotation release];    
    }
}

Is it possible to display the title for all pins without clicking on them?

Pang
  • 9,564
  • 146
  • 81
  • 122
Rowie Po
  • 1,339
  • 4
  • 13
  • 17

5 Answers5

7

Declare an array to store all annotation and use MKMapView's setSelectedAnnotations:(NSArray *) method

- (void)addAnnotations {

    NSMutableArray *annotationArray = [[NSMutableArray alloc]init];
    NSInteger i;
    CLLocationCoordinate2D location;

    for ( i = 0 ; i < [storeLatitude count] ; i ++ ) {
        location.latitude = [[storeLatitude objectAtIndex:i] floatValue];
        location.longitude = [[storeLongitude objectAtIndex:i] floatValue];

        MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:    [listOfStores objectAtIndex:i] andCoordinate:location];
        [annotationArray addObject:newAnnotation];
        [self.mapView addAnnotation:newAnnotation];    
    }
    [mapView setSelectedAnnotations:annotationArray];
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Ab'initio
  • 5,368
  • 4
  • 28
  • 40
4

Try this. It will show the title automatically.

-(void)mapView:(MKMapView *)mapView1 didAddAnnotationViews:(NSArray *)views
{
    [self.mapView selectAnnotation:yourannotationpin animated:NO];
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Madhumitha
  • 3,794
  • 8
  • 30
  • 45
0

U May use custom annotation view for displaying Title and subTitle without click on pin. there is an tutorial for custom annotation view.

This app contains the custom annotation view

R_RKumawat
  • 133
  • 15
0

Unable to find the swift version of setSelectedAnnotations. Below shows the title, however for multiple moving targets only the title of the last in the array will be shown until the next iteration of didAdd. So still looking for a more general/complete solution.

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
    for view in views {
        if let annotation = view.annotation, let title = annotation.title {
            print("displaying:\(title!)")
            mapView.selectAnnotation( annotation, animated: true )
        }
    }
}
eklektek
  • 1,083
  • 1
  • 16
  • 31
-3

no its not possible. you must click on pin Annotation to display title.

Victor
  • 127
  • 1
  • 8