2

I am loading Multiple annotations onto my map view. These annotations displays as a pin when the map is loaded.

By using the below code i can display title directly for one annotation, but for remaining annotations user is required to tap on the pin for title to see.

I want to display the title for all the annotations when the Mapview Loaded without touching the annotation pin

NSMutableArray * locations = [[NSMutableArray alloc] init];

_myAnn = [[MKPointAnnotation alloc] init];
_locationCoordinate.latitude = 27.175015;
_locationCoordinate.longitude = 78.042155;
_myAnn.coordinate = _locationCoordinate;
_myAnn.title = @"A";
[locations addObject:_myAnn];

_myAnn = [[MKPointAnnotation alloc] init];
_locationCoordinate.latitude = 28.171391;
_locationCoordinate.longitude = 79.037090;
_myAnn.coordinate = _locationCoordinate;
_myAnn.title = @"B";
[locations addObject:_myAnn];

_myAnn = [[MKPointAnnotation alloc] init];
_locationCoordinate.latitude = 29.169005;
_locationCoordinate.longitude = 80.043206;
_myAnn.coordinate = _locationCoordinate;
_myAnn.title = @"C ";
[locations addObject:_myAnn];

[self.map_View addAnnotations:locations];

  // i am using below method to display title for one annotation

      // [self.map_View selectAnnotation:_myAnn animated:YES];

     for (MKPointAnnotation *annotation in locations) {
  [self.map_View selectAnnotation:annotation animated:NO];
  }

MKCoordinateSpan span;
Span. latitudeDelta = 10;
Span. longitudeDelta = 10;

MKCoordinateRegion region;

region.span = span;
region.center = _locationCoordinate;

[self.map_View setRegion:region animated:YES];

Thanks in Advance..

  • The MKMapView can only show one callout at a time. To show some text on all the annotations without tapping on them, you'll need to customize the annotation view itself (ie. not rely on the callout). For examples, see http://stackoverflow.com/questions/9822756/replace-icon-pin-by-text-label-in-annotation, http://stackoverflow.com/questions/6175670/add-labeltext-on-mkannotation-image, http://stackoverflow.com/questions/18457975/how-to-add-annotation-in-mapview-with-image-and-label-in-iphone, etc. –  Jun 19 '14 at 02:19

2 Answers2

1

Update It does indeed seem that this isn't possible - at least not with the map view annotation APIs. Despite the existence of the select and deselect methods and the selected annotations property being an array, it seems that only a single annotation can be selected at a time.

It looks like you will need to create a custom annotation view as suggested in this answer - Multiple annotation callouts displaying in MKMapView

Community
  • 1
  • 1
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • thanks.. For the replay.. But that one you are suggesting is not working... If I execute the code by adding above for loop then also... Title visible for only last annotation.. For first and second annotation titles I have to click on it...... Plz check that.. If it's possible update me also.. – Pavan Chowdary Jun 18 '14 at 18:04
  • Did you ensure that your visible map range included all annotations before selecting them? – Paulw11 Jun 18 '14 at 22:26
  • Otherwise you will need to call the selection when the annotation view becomes visible - see my updated answer – Paulw11 Jun 19 '14 at 02:04
  • Thanks For Your Replay again...but i comes to know in only one annotation title will be displayed at a time.. – Pavan Chowdary Jun 19 '14 at 22:43
0

According to the MKAnnotationView Class Reference, annotations callout bubbles appear & disappear upon change value of the selected property. The document states that developers "should not set the value of this property directly", however you can try :)

The first what I'd try is a simplest hacky way relying on that MKAnnotationView property. In your MKMapViewDelegate class, define the following method:

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    view.selected = YES;
}

But I guess this may not work well because of visual artifacts upon tapping different annotations. If this is true, then the only proper way to achieve what you want is to create subclass of the MKAnnotationView which always displays a callout bubble regardless of a value of the selected property. Here are some tutorials on how to create a custom annotation view:

Update

Thanks to the @Paulw11's answer, I forgot to mention that to make the 'hacky' approach working, you need to set the select value for all annotations. This can be done in 3 ways:

  1. As per @Paulw11's answer (but this will not work for annotations outside of map's visible rectangle)
  2. Set the MKAnnotationView's selected property in your implementation of MKMapViewDelegate's mapView:viewForAnnotation: method (not sure if this will work)
  3. Set the MKAnnotationView's selected property for visible annotations each time when map view visible region changes.

With 3rd approach, I you'll need to implement the MKMapViewDelegate's mapView:regionDidChangeAnimated: method handling changes of map view visible region. Since this delegate method is called very often while a user is dragging or zooming in/out, it makes sense to defer actual update of annotation views until map view's visible region changes are over; to do this, I'd use NSTimer.

// In an interface or interface extension for MKMapViewDelegate class (guess this
// is your view controller), declare NSTimer and a method which will be executed
// when a user stops dragging or zooming
@property (nonatomic, strong) NSTimer* mapViewRegionChangeTimer;
- (void)mapViewRegionChangeCompleted:(NSTimer*)timer;


// In your implementation of MKMapViewDelegate:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    if (self.mapViewRegionChangeTimer != nil) {
        [self.mapViewRegionChangeTimer invalidate];
    }
    self.mapViewRegionChangeTimer = 
        [NSTimer scheduledTimerWithTimeInterval:0.2
                                         target:self
                                       selector:@selector(mapViewRegionChangeCompleted:)
                                       userInfo:nil
                                        repeats:NO];
}

- (void)mapViewRegionChangeCompleted:(NSTimer*)timer
{
    [self.mapViewRegionChangeTimer invalidate];
    self.mapViewRegionChangeTimer = nil;

    NSSet* visibleAnnotations = [self.map_View annotationsInMapRect:self.map_View.visibleMapRect];
    for (id<MKAnnotation> annotation in visibleAnnotations) {
        [self.map_View selectAnnotation:annotation animated:NO];
    }
}
Community
  • 1
  • 1
Nikolay Mamaev
  • 1,474
  • 1
  • 12
  • 21
  • Thanks for trying to help me... But .. My task requirement is like...if I am going to display 3 annotatios all the 3 annotation titles should be display by default not by clicking on the annotation title. through my code i can display only for one at a time but i want all the three should be displayed at a time – Pavan Chowdary Jun 18 '14 at 21:47
  • You can set `selected` property for all your annotation views by calling `[self.map_View selectAnnotation:animated:]` upon changing map's visible region, and this should solve your task. But if user clicks map in some place not occupied by annotations then probably annotations will be deselected automatically; or, if user clicks one of annotations then I guess another ones may be deselected automatically as well. That's why I think that you'll need to implement `mapView:didDeselectAnnotationView:` method to make sure that if an annotation is deselected automatically then it gets selected back. – Nikolay Mamaev Jun 18 '14 at 22:43