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..
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
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:
- http://bakyelli.wordpress.com/2013/10/13/creating-custom-map-annotations-using-mkannotation-protocol/
- http://blog.asynchrony.com/2010/09/building-custom-map-annotation-callouts-part-1/, http://blog.asynchrony.com/2010/09/building-custom-map-annotation-callouts-part-2/
- http://www.codigator.com/tutorials/advanced-mapkit-tutorial-for-ios-custom-callout/
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:
- As per @Paulw11's answer (but this will not work for annotations outside of map's visible rectangle)
- Set the
MKAnnotationView
'sselected
property in your implementation ofMKMapViewDelegate
'smapView:viewForAnnotation:
method (not sure if this will work) - Set the
MKAnnotationView
'sselected
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];
}
}
来源:https://stackoverflow.com/questions/24253631/how-to-display-title-for-multiple-annotations-directly-when-map-loaded