我正在编写一个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
答案 0 :(得分:2)
就个人而言,我倾向于这样做:
首先,我倾向于保存我传递给CLLocationCoordinate2D
实例方法MKPolygon
的{{1}}坐标的C数组(而不是polygonWithCoordinates
C数组)。如果你宁愿使用MKMapPoint
,你可以调整这段代码,但我更喜欢一种我可以在外部检查并理解的格式(即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
或者,你可以用plist格式读取和写入- (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];
}
数组(以人类可读的格式呈现),方法是将上述两种方法替换为:
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];
}
和writeToFile
(或dataWithContentsOfFile
)成功,但希望这能为您提供这个想法。