-
Notifications
You must be signed in to change notification settings - Fork 0
/
KMLPlacemark.m
189 lines (154 loc) · 4.71 KB
/
KMLPlacemark.m
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// KMLPlacemark.m
// KMLViewer
//
// Created by Mojtaba Cazi on 2/27/18.
//
#import "KMLPlacemark.h"
#import "KMLPoint.h"
#import "KMLLineString.h"
#import "kml_com.h"
@interface KMLPlacemark ()
// Corresponds to the title property on MKAnnotation
@property (nonatomic, strong) NSString *name;
// Corresponds to the subtitle property on MKAnnotation
@property (nonatomic, strong) NSString *placemarkDescription;
@property (nonatomic, strong) NSMutableArray <KMLGeometry *> *geometry;
@property (unsafe_unretained, nonatomic) KMLPolygon *polygon;
@property (nonatomic, strong) NSString *styleUrl;
@property (NS_NONATOMIC_IOSONLY, strong) NSArray <MKOverlay> *overlays;
@property (NS_NONATOMIC_IOSONLY, strong) NSArray <MKAnnotation> *points;
@end
typedef struct {
int inName:1;
int inDescription:1;
int inStyle:1;
int inGeometry:1;
int inStyleUrl:1;
} KMLFlagType;
@implementation KMLPlacemark {
MKAnnotationView *_annotationView;
MKOverlayPathRenderer *_overlayPathRenderer;
KMLFlagType _flags;
}
- (instancetype)initWithIdentifier:(NSString *)ident
{
self = [super initWithIdentifier:ident];
if (self) {
_geometry = [NSMutableArray array];
}
return self;
}
- (BOOL)canAddString {
return _flags.inName || _flags.inStyleUrl || _flags.inDescription;
}
- (void)addString:(NSString *)str {
if (_flags.inStyle) {
[_style addString:str];
} else if (_flags.inGeometry) {
[_geometry.lastObject addString:str];
} else {
[super addString:str];
}
}
- (void)beginName {
_flags.inName = YES;
}
- (void)endName {
_flags.inName = NO;
_name = [accum copy];
[self clearString];
}
- (void)beginDescription {
_flags.inDescription = YES;
}
- (void)endDescription {
_flags.inDescription = NO;
_placemarkDescription = [accum copy];
[self clearString];
}
- (void)beginStyleUrl {
_flags.inStyleUrl = YES;
}
- (void)endStyleUrl {
_flags.inStyleUrl = NO;
_styleUrl = [accum copy];
[self clearString];
}
- (void)beginStyleWithIdentifier:(NSString *)ident {
_flags.inStyle = YES;
_style = [[KMLStyle alloc] initWithIdentifier:ident];
}
- (void)endStyle {
_flags.inStyle = NO;
}
- (void)beginGeometryOfType:(NSString *)elementName withIdentifier:(NSString *)ident {
_flags.inGeometry = YES;
if (ELTYPE(Point)) {
[_geometry addObject:[[KMLPoint alloc] initWithIdentifier:ident]];
} else if (ELTYPE(Polygon)) {
[_geometry addObject:[[KMLPolygon alloc] initWithIdentifier:ident]];
} else if (ELTYPE(LineString)) {
[_geometry addObject:[[KMLLineString alloc] initWithIdentifier:ident]];
}
}
- (void)endGeometry {
_flags.inGeometry = NO;
}
- (NSArray <id<MKOverlay>> *)overlays {
if (_overlays) {
return _overlays;
}
NSMutableArray *shapes = [NSMutableArray array];
for (KMLGeometry *geometry in _geometry) {
MKShape *shape = [geometry mapkitShape];
shape.title = _name;
// Skip setting the subtitle for now because they're frequently
// too verbose for viewing on in a callout in most kml files.
// mkShape.subtitle = placemarkDescription;
if ([shape conformsToProtocol:@protocol(MKOverlay)]) {
[shapes addObject:shape];
}
}
_overlays = [shapes copy];
return _overlays;
}
- (NSArray <id<MKAnnotation>> *)points {
if (_points) {
return _points;
}
// Make sure to check if this is an MKPointAnnotation. MKOverlays also
// conform to MKAnnotation, so it isn't sufficient to just check to
// conformance to MKAnnotation.
NSMutableArray *shapes = [NSMutableArray array];
for (KMLGeometry *geometry in _geometry) {
MKShape *shape = [geometry mapkitShape];
shape.title = _name;
// Skip setting the subtitle for now because they're frequently
// too verbose for viewing on in a callout in most kml files.
// mkShape.subtitle = placemarkDescription;
if ([shape isKindOfClass:[MKPointAnnotation class]]) {
[shapes addObject:shape];
}
}
_points = [shapes copy];
return _points;
}
- (MKOverlayPathRenderer *)overlayPathRendererForOverlay:(id)overlay
{
NSUInteger idx = [self.overlays indexOfObject:overlay];
if (idx >= _geometry.count) {
return nil;
}
MKOverlayPathRenderer *renderer = [_geometry[idx] createOverlayPathRenderer:overlay];
[_style applyToOverlayPathRenderer:renderer];
return renderer;
}
- (MKAnnotationView *)annotationViewForPoint:(id)point
{
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:point reuseIdentifier:nil];
pin.canShowCallout = YES;
pin.animatesDrop = YES;
return pin;
}
@end