Reputation: 1419
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?
Upvotes: 5
Views: 3806
Reputation: 1173
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 )
}
}
}
Upvotes: 0
Reputation: 3824
Try this. It will show the title automatically.
-(void)mapView:(MKMapView *)mapView1 didAddAnnotationViews:(NSArray *)views
{
[self.mapView selectAnnotation:yourannotationpin animated:NO];
}
Upvotes: 4
Reputation: 5418
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];
}
Upvotes: 7
Reputation: 133
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
Upvotes: 0
Reputation: 127
no its not possible. you must click on pin Annotation to display title.
Upvotes: -3