【发布时间】:2014-03-03 19:00:35
【问题描述】:
我有一张地图,我可以在上面添加多个带有可自定义字幕的注释。我想将注释放入带有更新字幕的数组中。
目前,当我添加注释时,它会进入数组,但我不知道如何使用字幕更新数组,如果注释从地图中删除,则从数组中删除注释。
我还想在切换到下一个视图时将所有注释添加到数组中,但不知道该怎么做。
我当前的代码:
使用 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);
编辑字幕并从警报视图中删除注释
- (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];
}
}
【问题讨论】:
-
mapAnnotationArray 是做什么用的?如果您将
mapPoint注释对象本身而不是self.mapAnnotation字符串添加到数组中,将会简单得多。 -
@Anna 我将把
mapAnnotationArray传递到一个MySQL 数据库中,稍后我可以从中检索,带有标题和副标题的经纬度坐标,并将注释绘制到另一个地图视图上,mapPoint不是t 像 NSString 那样向我展示完整的细节。 -
您要将注释数据作为逗号分隔的字符串存储在数据库中吗?不建议这样做。尽管如此,当您将数据保存在数组中时,您不需要将其存储为格式化字符串。您可以在
MapAnnotation类中添加一个description方法覆盖,该类根据该类的不同属性按需输出格式化的字符串。通过将数据存储为分隔字符串,您必须不断解析它以检索和搜索值。 -
一个单独的潜在问题是注释编号方案:如果删除了注释,那么以后添加的新注释可以获得与已删除或现有注释相同的编号,从而导致冲突/重复。在继续之前,您可能需要重新考虑整体设计/方法。
-
@Anna 我通过 php 将数组发送到数据库,php 文件会删除任何不需要的字符并存储要检索的字符串并使用我使用的代码放置到地图上,目前这正在工作。我对此只有两个问题,如果我向数组添加注释,我无法将其删除,也无法将编辑后的字幕添加到数组中。我确实理解您提到的方式更适合使用,特别是如果它可以帮助我解决这两个问题,但我不知道如何实现。
标签: ios nsmutablearray mkmapview mkannotation