通过NSCoding读取后,自定义MkPointAnnotation无法正确加载

时间:2015-02-16 03:40:51

标签: ios objective-c

我有这个最糟糕的时间。我有一个MKMapView,当用户执行长按时,我在其中添加注释,这是有效的。用户可以单击注释并显示一个漂亮的标注。但是,如果我存储了注释并使用NSCoding协议将它们加载回来,它们会出现在正确的坐标中,尽管它们是不可点击的。

Annotation类的编码:

- (void) encodeWithCoder:(NSCoder *)encoder {

    [encoder encodeInteger:_type forKey:kType];
    [encoder encodeObject:_note forKey:kNote];

    NSMutableArray* coord = [[NSMutableArray alloc]init];
    [coord addObject:[NSNumber numberWithDouble:self.coordinate.latitude]];
    [coord addObject:[NSNumber numberWithDouble:self.coordinate.longitude]];

    [encoder encodeObject:coord forKey:kCoordinate];
}

- (id)initWithCoder:(NSCoder *)decoder {

    NSString* note = [decoder decodeObjectForKey:kNote];
    NSInteger type = [decoder decodeIntegerForKey:kType];
    NSMutableArray* coord = [decoder decodeObjectForKey:kCoordinate];

    double lat = [(NSString *)[coord objectAtIndex:0] doubleValue];
    double lon = [(NSString *)[coord objectAtIndex:1] doubleValue];

    CLLocationCoordinate2D coords = (CLLocationCoordinate2D){ lat, lon };
    return [self initWithAttrs:coords note:note type:type];
}

加载数据并存储在NSMutableArray

- (void) saveData {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"appData"];

    [NSKeyedArchiver archiveRootObject:self.annotations toFile:filePath];
}

- (void) loadData {
    // look for saved data.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"appData"];

    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSData *data = [NSData dataWithContentsOfFile:filePath];
        NSArray *savedData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        self.annotations = [[NSMutableArray alloc] initWithArray:savedData];
    }
}

现在,哪些有效,哪些无效。我很难过! 好的,这很有效。一个引脚被丢弃,用户可以整天点击该引脚并显示标注

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
        return;

    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
    CLLocationCoordinate2D touchMapCoordinate =
        [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];

    Annotation* a = [[Annotation alloc]init];
    a.coordinate = touchMapCoordinate;
    a.title = @"test2";
    [self.annotations addObject:a];

    [self.mapView addAnnotation:a];
    [self saveData];
}

这不起作用。它应该有效地完成与长按期间完全相同的事情。有人请赐教(我意识到还有'addAnnotations'方法)。

- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered
{
    for(Annotation* anno in self.annotations){
        [self.mapView addAnnotation:anno];
    }
}

1 个答案:

答案 0 :(得分:0)

好的,经过相当长的时间调试后,我找到了答案,我就要杀了一些东西。 Opps,可能不允许在这里吗?

在任何情况下,如果注释没有设置标题,则标注不会显示。这是简单的事情......