Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions GPXParser/MKPolyline+NSCoding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// MKPolyline+NSCoding.h
// Vandy Vans
//
// Created by Seth Friedman on 1/4/14.
// Copyright (c) 2014 VandyApps. All rights reserved.
//

#import <MapKit/MapKit.h>

@interface MKPolyline (NSCoding) <NSCoding>

@end
69 changes: 69 additions & 0 deletions GPXParser/MKPolyline+NSCoding.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// MKPolyline+NSCoding.m
// Vandy Vans
//
// Created by Seth Friedman on 1/4/14.
// Copyright (c) 2014 VandyApps. All rights reserved.
//
// @see: https://github.com/VandyApps/vandyvans-ios/blob/master/Vandy%20Vans/MKPolyline%2BNSCoding.m

#import "MKPolyline+NSCoding.h"

static NSString * const kXKey = @"x";
static NSString * const kYKey = @"y";
static NSString * const kTitleKey = @"title";
static NSString * const kSubtitleKey = @"subtitle";

@interface MKMultiPoint ()

@property (nonatomic, readwrite) MKMapPoint *points;

@end

@implementation MKPolyline (NSCoding)

- (id)initWithCoder:(NSCoder *)aDecoder {
NSArray *xArray = [aDecoder decodeObjectForKey:kXKey];
NSArray *yArray = [aDecoder decodeObjectForKey:kYKey];

NSUInteger count = [xArray count];
MKMapPoint pointArray[count];

for (NSUInteger i = 0; i < count; ++i) {
pointArray[i] = MKMapPointMake([xArray[i] doubleValue], [yArray[i] doubleValue]);
}

self = [self.class polylineWithPoints:pointArray
count:count];

if (self) {
self.title = [aDecoder decodeObjectForKey:kTitleKey];
self.subtitle = [aDecoder decodeObjectForKey:kSubtitleKey];
}

return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
NSMutableArray *xArray = [NSMutableArray arrayWithCapacity:self.pointCount];
NSMutableArray *yArray = [NSMutableArray arrayWithCapacity:self.pointCount];

for (NSUInteger i = 0; i < self.pointCount; ++i) {
MKMapPoint point = self.points[i];

[xArray addObject:@(point.x)];
[yArray addObject:@(point.y)];
}

[aCoder encodeObject:[xArray copy]
forKey:kXKey];
[aCoder encodeObject:[yArray copy]
forKey:kYKey];

[aCoder encodeObject:self.title
forKey:kTitleKey];
[aCoder encodeObject:self.subtitle
forKey:kSubtitleKey];
}

@end
2 changes: 1 addition & 1 deletion GPXParser/Models/Fix.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface Fix : NSObject
@interface Fix : NSObject <NSCoding>
@property (nonatomic, assign) double latitude;
@property (nonatomic, assign) double longitude;

Expand Down
20 changes: 18 additions & 2 deletions GPXParser/Models/Fix.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#import "Fix.h"

@implementation Fix
@synthesize latitude=_latitude;
@synthesize longitude=_longitude;

#pragma mark - Coordinate

Expand All @@ -24,4 +22,22 @@ - (NSString *)description {
return [NSString stringWithFormat:@"<Fix (%f %f)>", _latitude, _longitude];
}

#pragma mark - NSCoding

- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeDouble:self.latitude forKey:@"latitude"];
[coder encodeDouble:self.longitude forKey:@"longitude"];
}

- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super init])
{
_latitude = [decoder decodeDoubleForKey:@"latitude"];
_longitude = [decoder decodeDoubleForKey:@"longitude"];
}
return self;
}

@end
2 changes: 1 addition & 1 deletion GPXParser/Models/GPX.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface GPX : NSObject
@interface GPX : NSObject <NSCoding>
@property (nonatomic, strong) NSMutableArray *waypoints;
@property (nonatomic, strong) NSMutableArray *tracks;
@property (nonatomic, strong) NSMutableArray *routes;
Expand Down
40 changes: 34 additions & 6 deletions GPXParser/Models/GPX.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
#import "GPX.h"

@implementation GPX
@synthesize tracks=_tracks;
@synthesize routes=_routes;
@synthesize waypoints=_waypoints;
@synthesize filename=_filename;
@synthesize region=_region;

#pragma mark - Initialization

Expand All @@ -29,7 +24,40 @@ - (id)init {
#pragma mark - String

- (NSString *)description {
return [NSString stringWithFormat:@"<GPX (tracks %i routes %i waypoints %i)>", _tracks.count, _routes.count, _waypoints.count];
return [NSString stringWithFormat:@"<GPX (tracks %lu routes %lu waypoints %lu)>", (unsigned long)_tracks.count, (unsigned long)_routes.count, (unsigned long)_waypoints.count];
}

#pragma mark - NSCoding

- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.tracks forKey:@"tracks"];
[coder encodeObject:self.routes forKey:@"routes"];
[coder encodeObject:self.waypoints forKey:@"waypoints"];
[coder encodeObject:self.filename forKey:@"filename"];

[coder encodeDouble:self.distance forKey:@"distance"];

// region to NSData
NSData *regionData = [NSData dataWithBytes:&_region length:sizeof(self.region)];
[coder encodeObject:regionData forKey:@"region"];
}

- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super init])
{
_tracks = [decoder decodeObjectForKey:@"tracks"];
_routes = [decoder decodeObjectForKey:@"routes"];
_waypoints = [decoder decodeObjectForKey:@"waypoints"];
_filename = [decoder decodeObjectForKey:@"filename"];

_distance = [decoder decodeDoubleForKey:@"distance"];

NSData *regionData = [decoder decodeObjectForKey:@"region"];
[regionData getBytes:&_region length:sizeof(self.region)];
}
return self;
}

@end
2 changes: 1 addition & 1 deletion GPXParser/Models/Track.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface Track : NSObject
@interface Track : NSObject <NSCoding>
@property (nonatomic, strong) NSMutableArray *fixes;
@property (nonatomic, strong) MKPolyline *path;
@property (nonatomic, strong) MKPolyline *shadowPath;
Expand Down
34 changes: 29 additions & 5 deletions GPXParser/Models/Track.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
//

#import "Track.h"
#import "MKPolyline+NSCoding.h"


@implementation Track
@synthesize region=_region;
@synthesize fixes=_fixes;
@synthesize path=_path;
@synthesize shadowPath=_shadowPath;

#pragma mark - Initialization

Expand All @@ -26,7 +24,33 @@ - (id)init {
#pragma mark - String

- (NSString *)description {
return [NSString stringWithFormat:@"<Track (fixes %i)>", _fixes.count];
return [NSString stringWithFormat:@"<Track (fixes %lu)>", (unsigned long)_fixes.count];
}

#pragma mark - NSCoding

- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.fixes forKey:@"fixes"];
[coder encodeObject:self.path forKey:@"path"];
[coder encodeObject:self.shadowPath forKey:@"shadowPath"];

NSData *regionData = [NSData dataWithBytes:&_region length:sizeof(self.region)];
[coder encodeObject:regionData forKey:@"region"];
}

- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super init])
{
_fixes = [decoder decodeObjectForKey:@"fixes"];
_path = [decoder decodeObjectForKey:@"path"];
_shadowPath = [decoder decodeObjectForKey:@"shadowPath"];

NSData *regionData = [decoder decodeObjectForKey:@"region"];
[regionData getBytes:&_region length:sizeof(self.region)];
}
return self;
}

@end
6 changes: 5 additions & 1 deletion GPXParser/Models/Waypoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

#import "Fix.h"

@interface Waypoint : Fix
@interface Waypoint : Fix <NSCoding>

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *desc;
@property (nonatomic, strong) NSString *comment;
@property (nonatomic, strong) NSString *type;

@end
26 changes: 24 additions & 2 deletions GPXParser/Models/Waypoint.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,35 @@
#import "Waypoint.h"

@implementation Waypoint
@synthesize name=_name;
@synthesize type=_type;

#pragma mark - String

- (NSString *)description {
return [NSString stringWithFormat:@"<Waypoint (%@ %f %f)>", _name, self.latitude, self.longitude];
}

#pragma mark - NSCoding

- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];

[coder encodeObject:self.name forKey:@"name"];
[coder encodeObject:self.desc forKey:@"desc"];
[coder encodeObject:self.comment forKey:@"comment"];
[coder encodeObject:self.type forKey:@"type"];
}

- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super initWithCoder:decoder])
{
_name = [decoder decodeObjectForKey:@"name"];
_desc = [decoder decodeObjectForKey:@"desc"];
_comment = [decoder decodeObjectForKey:@"comment"];
_type = [decoder decodeObjectForKey:@"type"];
}
return self;
}

@end
22 changes: 22 additions & 0 deletions GPXParser/Parsers/GPXParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName nam
self.currentString = [NSMutableString string];
}

// Waypoint name
if ([elementName isEqualToString:@"name"] && self.waypoint) {
self.currentString = [NSMutableString string];
}

// Waypoint comment
if ([elementName isEqualToString:@"cmt"] && self.waypoint) {
self.currentString = [NSMutableString string];
}

// Route
if ([elementName isEqualToString:@"rte"]) {
if (!self.route) self.route = [Track new];
Expand Down Expand Up @@ -73,10 +83,22 @@ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName names

// Waypoint name
if ([elementName isEqualToString:@"desc"] && self.waypoint) {
self.waypoint.desc = self.currentString;
self.currentString = nil;
}

// Waypoint name
if ([elementName isEqualToString:@"name"] && self.waypoint) {
self.waypoint.name = self.currentString;
self.currentString = nil;
}

// Waypoint comment
if ([elementName isEqualToString:@"cmt"] && self.waypoint) {
self.waypoint.comment = self.currentString;
self.currentString = nil;
}

// End waypoint
if([elementName isEqualToString:@"wpt"] && self.waypoint) {
[self.gpx.waypoints addObject:self.waypoint];
Expand Down