MKAnnotation display all pin titles without clicking
Asked Answered
M

5

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?

Morbihan answered 5/7, 2012 at 9:46 Comment(0)
R
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];
}
Reft answered 5/7, 2012 at 10:32 Comment(0)
T
4

Try this. It will show the title automatically.

-(void)mapView:(MKMapView *)mapView1 didAddAnnotationViews:(NSArray *)views
{
    [self.mapView selectAnnotation:yourannotationpin animated:NO];
}
Technic answered 5/7, 2012 at 10:9 Comment(0)
B
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

Bendy answered 5/7, 2012 at 10:7 Comment(1)
Link in answer is dead - Sorry, that page cannot be foundParamagnet
C
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 )
        }
    }
}
Comparable answered 16/5, 2018 at 19:7 Comment(0)
I
-3

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

Irinairis answered 5/7, 2012 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.