I have a map on which I can add multiple annotations with a customisable subtitle. I want to put the annotations into an array with the updated subtitle.
Currently when I add an annotation it goes into the array, but I don't know how to update the array with the subtitle and remove the annotation from the array if it gets removed from the map.
I was also thinking to add all the annotations to the array when switching to the next view, but not sure how to do that.
My current code:
Adding the annotation with a UIGestureRecognizer
MapAnnotation *mapPoint = [[MapAnnotation alloc]init];
mapPoint.coordinate = touchMapCoordinate;
mapPoint.title = address;
mapPoint.subtitle = @"";
[self.map addAnnotation:mapPoint];
self.mapLatitudeString = [NSString stringWithFormat:@"%f",mapPoint.coordinate.latitude];
self.mapLongitudeString = [NSString stringWithFormat:@"%f",mapPoint.coordinate.longitude];
self.mapTitleString = [NSString stringWithFormat:@"%@", mapPoint.title];
self.mapSubtitleString = [NSString stringWithFormat:@"%@", mapPoint.subtitle];
self.mapAnnotation = [NSString stringWithFormat:@"%d,%@,%@,%@,%@",self.mapAnnotationArray.count, self.mapLatitudeString, self.mapLongitudeString, self.mapTitleString, self.mapSubtitleString];
if (!self.mapAnnotationArray) {
self.mapAnnotationArray = [[NSMutableArray alloc] init];
}
[self.mapAnnotationArray addObject:self.mapAnnotation];
NSLog(@"%@", self.mapAnnotationArray);
Editing the subtitle and removing the annotation from an alert view
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
}
if (buttonIndex == 1) {
if (self.map.selectedAnnotations.count > 0)
{
id<MKAnnotation> ann = [self.map.selectedAnnotations objectAtIndex:0];
if ([ann isKindOfClass:[MapAnnotation class]])
{
MKPointAnnotation *pa = (MKPointAnnotation *)ann;
pa.subtitle = [[alertView textFieldAtIndex:0] text];
}
}
}
if (buttonIndex == 2) {
[self.map removeAnnotations:self.map.selectedAnnotations];
}
}