我正在编写一个 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',原因:'-[NSConcreteData isEqualToString:]:无法识别的选择器发送到实例
最佳答案
就我个人而言,我倾向于这样做:
首先,我倾向于保存传递给
MKPolygon
实例方法polygonWithCoordinates
的CLLocationCooperative2D
坐标的 C 数组(而不是MKMapPoint
C 数组)。如果您更愿意使用MKMapPoints
,则可以调整此代码,但我更喜欢一种可以在外部检查并理解的格式(即CLLocationCooperative2D
的纬度和经度值) )。因此,我们假设您使用如下代码来制作MKPolygon
:MKPolygon* poly = [MKPolygon polygonWithCoordinates:coordinates count:count];
因此,您可以像这样保存坐标:
[self writeCoordinates:coordinates count:count file:filename];
其中
writeCooperatives: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]; }
或者,您可以以 plist 格式(以人类可读的格式呈现)读取和写入
CLLocationCooperative2D
数组,方法是将上述两种方法替换为: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]; }
显然,您希望添加错误检查代码以确保 writeToFile
和 dataWithContentsOfFile
(或 arrayWithContentsOfFile
)成功,但是希望这能给您带来启发。
关于ios - 如何将 MKOverlay 对象存储到文件以便稍后检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15149484/