2

我正在编写一个 iOS 应用程序来记录您的旅行路径,然后将其存储起来以便以后检索。大多数绘制路径的代码都是基于示例代码Breadcrumb

现在我正在添加功能来保存绘制的叠加层。最好的方法是什么?我可以使用CoreData,但我不想对绘制的叠加层做太多事情,而不是稍后再检索它。我目前尝试了一个简单的NSArray. 我将 CrumbPath 对象转换为NSData并将其存储在数组中。后来我检索它并将其转换回 CrumbPath。但是,我似乎做错了什么。

@interface CrumbPath : NSObject <MKOverlay>
{
    MKMapPoint *points;
    NSUInteger pointCount;
    NSUInteger pointSpace;

    MKMapRect boundingMapRect;

    pthread_rwlock_t rwLock;
}

- (id)initWithCenterCoordinate:(CLLocationCoordinate2D)coord;
- (MKMapRect)addCoordinate:(CLLocationCoordinate2D)coord;

@property (readonly) MKMapPoint *points;
@property (readonly) NSUInteger pointCount;

@end

我像这样保存 CrumbPath 对象“crumbs”:

NSData *pointData = [NSData dataWithBytes:crumbs.points length:crumbs.pointCount * sizeof(MKMapPoint)];
[patternArray addObject:pointData];
[timeArray addObject:date];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Create the full file path by appending the desired file name
NSString *patternFile = [documentsDirectory stringByAppendingPathComponent:@"patterns.dat"];
NSString *timeFile = [documentsDirectory stringByAppendingPathComponent:@"times.dat"];

//Save the array
[patternArray writeToFile:patternFile atomically:YES];
[timeArray writeToFile:timeFile atomically:YES];

稍后像这样检索它以显示在表格中:

NSData *pointData = [appDelegate.patternArray objectAtIndex:indexPath.row];
MKMapPoint *points = malloc(pointData.length);
(void)memcpy([pointData bytes], points, sizeof(pointData));

并再次构建路径:

crumbs = [[CrumbPath alloc] initWithCenterCoordinate:MKCoordinateForMapPoint(points[0])];
for (int i = 1; i <= pointCount; i++) {
    [crumbs addCoordinate:MKCoordinateForMapPoint(points[i])];
}    
[map addOverlay:crumbs];

但是,我收到一个错误:'NSInvalidArgumentException', reason: '-[NSConcreteData isEqualToString:]: unrecognized selector sent to instance

4

1 回答 1

2

就个人而言,我倾向于这样做:

  1. 首先,我倾向于保存CLLocationCoordinate2D传递给MKPolygon实例方法的 C 坐标数组polygonWithCoordinates(而不是MKMapPointC 数组)。如果您更愿意使用MKMapPoints,您可以修改此代码,但我更喜欢一种我可以从外部检查并理解的格式(即 的纬度和经度值CLLocationCoordinate2D)。因此,让我们假设您MKPolygon使用如下代码行:

    MKPolygon* poly = [MKPolygon polygonWithCoordinates:coordinates count:count];
    

    因此,您可以像这样保存坐标:

    [self writeCoordinates:coordinates count:count file:filename];
    

    其中writeCoordinates:count:filename:定义如下:

    - (void)writeCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUInteger)count file:(NSString *)filename
    {
        NSData *data = [NSData dataWithBytes:coordinates length:count * sizeof(CLLocationCoordinate2D)];
        [data writeToFile:filename atomically:NO];
    }
    

    然后,您可以使用MKPolygon以下文件制作:

    MKPolygon* poly = [self polygonWithContentsOfFile:filename];
    

    其中polygonWithContentsOfFile定义为:

    - (MKPolygon *)polygonWithContentsOfFile:(NSString *)filename
    {
        NSData *data = [NSData dataWithContentsOfFile:filename];
        NSUInteger count = data.length / sizeof(CLLocationCoordinate2D);
        CLLocationCoordinate2D *coordinates = (CLLocationCoordinate2D *)data.bytes;
    
        return [MKPolygon polygonWithCoordinates:coordinates count:count];
    }
    
  2. 或者,您可以CLLocationCoordinate2D通过将上述两种方法替换为:

    const NSString *kLatitudeKey = @"latitude";
    const NSString *kLongitudeKey = @"longitude";
    
    - (void)writeCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUInteger)count file:(NSString *)filename
    {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:count];
    
        for (NSUInteger i = 0; i < count; i++)
        {
            CLLocationCoordinate2D coordinate = coordinates[i];
            [array addObject:@{kLatitudeKey:@(coordinate.latitude), kLongitudeKey:@(coordinate.longitude)}];
        }
    
        [array writeToFile:filename atomically:NO];
    }
    
    - (MKPolygon *)polygonWithContentsOfFile:(NSString *)filename
    {        
        NSArray *array = [NSArray arrayWithContentsOfFile:filename];
        NSUInteger count = [array count];
        CLLocationCoordinate2D coordinates[count];
    
        for (NSUInteger i = 0; i < count; i++)
        {
            NSDictionary *dictionary = array[i];
            coordinates[i].latitude = [dictionary[kLatitudeKey] doubleValue];
            coordinates[i].longitude = [dictionary[kLongitudeKey] doubleValue];
        }
    
        return [MKPolygon polygonWithCoordinates:coordinates count:count];
    }
    

显然,您希望添加错误检查代码以确保 thewriteToFiledataWithContentsOfFile(or arrayWithContentsOfFile) 成功,但希望这能给您带来想法。

于 2013-03-01T03:36:13.000 回答