-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGPXParser.m
More file actions
137 lines (120 loc) · 3.88 KB
/
GPXParser.m
File metadata and controls
137 lines (120 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// GPXParser.m
// Databáze letišť
//
// Created by Krystof Celba on 20.03.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "GPXParser.h"
#import "TBXML.h"
#import "GPXWaypointAnnotation.h"
#import "GPXTrackPolyLine.h"
#import "UIColor+named.h"
@implementation GPXParser
-(id) initWithGPXFile: (NSString *) file
{
if (self = [super init]) {
tbxml = [TBXML tbxmlWithXMLFile:file error:nil];
}
return self;
}
-(NSArray *) getWaypoints
{
NSMutableArray *wpts = [[NSMutableArray alloc] init];
TBXMLElement *root = tbxml.rootXMLElement;
if (root)
{
TBXMLElement *wpt = [TBXML childElementNamed:@"wpt" parentElement:root];
while (wpt)
{
TBXMLElement *nameEl = [TBXML childElementNamed:@"name" parentElement:wpt];
TBXMLElement *descEl = [TBXML childElementNamed:@"desc" parentElement:wpt];
TBXMLElement *symEl = [TBXML childElementNamed:@"sym" parentElement:wpt];
NSString *lat = [TBXML valueOfAttributeNamed:@"lat" forElement:wpt];
NSString *lon = [TBXML valueOfAttributeNamed:@"lon" forElement:wpt];
NSString *name = @"";
if (nameEl)
{
name = [TBXML textForElement:nameEl];
}
NSString *desc;
if (descEl)
{
desc = [TBXML textForElement:descEl];
}
NSString *image = @"";
if (symEl)
{
image = [[TBXML textForElement:symEl] stringByReplacingOccurrencesOfString:@" " withString:@"_"];
}
GPXWaypointAnnotation *ann = [[GPXWaypointAnnotation alloc] initWithName:name desc:desc coordinate:CLLocationCoordinate2DMake([lat doubleValue], [lon doubleValue])];
ann.imageName = image;
[wpts addObject:ann];
wpt = [TBXML nextSiblingNamed:@"wpt" searchFromElement:wpt];
}
}
return wpts;
}
-(NSArray *) getTracks
{
NSMutableArray *tracks = [[NSMutableArray alloc] init];
TBXMLElement *root = tbxml.rootXMLElement;
if (root)
{
TBXMLElement *trk = [TBXML childElementNamed:@"trk" parentElement:root];
while (trk)
{
//[self traverseElement:trk];
TBXMLElement *nameEl = [TBXML childElementNamed:@"name" parentElement:trk];
TBXMLElement *segEl = [TBXML childElementNamed:@"trkseg" parentElement:trk];
TBXMLElement *extEl = [TBXML childElementNamed:@"gpxx:TrackExtension" parentElement:[TBXML childElementNamed:@"extensions" parentElement:trk]];
NSString *name = @"";
if (nameEl)
{
name = [TBXML textForElement:nameEl];
}
UIColor *color = nil;
if (extEl)
{
TBXMLElement *colorEl = [TBXML childElementNamed:@"gpxx:DisplayColor" parentElement:extEl];
if (colorEl)
{
color = [UIColor colorNamed:[TBXML textForElement:colorEl]];
}
}
NSMutableArray *trackPoints = [[NSMutableArray alloc] init];
if (segEl)
{
TBXMLElement *pointEl = [TBXML childElementNamed:@"trkpt" parentElement:segEl];
while (pointEl)
{
NSString *lat = [TBXML valueOfAttributeNamed:@"lat" forElement:pointEl];
NSString *lon = [TBXML valueOfAttributeNamed:@"lon" forElement:pointEl];
CLLocation *loc = [[CLLocation alloc] initWithLatitude:[lat floatValue] longitude:[lon floatValue]];
[trackPoints addObject:loc];
pointEl = [TBXML nextSiblingNamed:@"trkpt" searchFromElement:pointEl];
}
CLLocationCoordinate2D coords[[trackPoints count]];
for(int i = 0; i < trackPoints.count; i++)
{
CLLocation* location = [trackPoints objectAtIndex:i];
CLLocationCoordinate2D c;
c.latitude = location.coordinate.latitude;
c.longitude = location.coordinate.longitude;
coords[i] = c;
}
GPXTrackPolyLine *poly = (GPXTrackPolyLine *)[GPXTrackPolyLine polylineWithCoordinates:coords count:trackPoints.count];
poly.color = [UIColor redColor];
if (color)
{
poly.color = color;
}
poly.title = name;
[tracks addObject:poly];
}
trk = [TBXML nextSiblingNamed:@"trk" searchFromElement:trk];
}
}
return tracks;
}
@end