/
ANAnnotation.m
141 lines (105 loc) · 3.36 KB
/
ANAnnotation.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
//
// ANAnnotation.m
// Appdate
//
// Created by Brent Royal-Gordon on 9/1/12.
// Copyright (c) 2012 Architechies. See README.md for licensing information.
//
#import "ANAnnotation.h"
#import "ANDraft.h"
NSString * const ANAnnotationTypeGeolocation = @"net.app.core.geolocation";
@implementation ANAnnotationSet {
NSArray * _all;
NSDictionary * _annotationsByType;
}
- (id)initWithRepresentation:(id)rep session:(ANSession *)session {
NSParameterAssert(session);
if((self = [super init])) {
_representation = rep;
_session = session;
}
return self;
}
- (NSArray *)all {
if(!_all) {
[self buildAnnotations];
}
return _all;
}
- (NSArray *)types {
if(!_annotationsByType) {
[self buildAnnotations];
}
return [_annotationsByType allKeys];
}
- (NSArray *)annotationsOfType:(NSString *)type {
if(!_annotationsByType) {
[self buildAnnotations];
}
return [_annotationsByType objectForKey:type];
}
- (void)buildAnnotations {
NSMutableArray * annotations = [NSMutableArray new];
NSMutableDictionary * byType = [NSMutableDictionary new];
for(NSDictionary * rep in self.representation) {
ANAnnotation * annotation = [[ANAnnotation alloc] initWithRepresentation:rep session:self.session];
[annotations addObject:annotation];
if(![byType objectForKey:annotation.type]) {
[byType setObject:[NSMutableArray new] forKey:annotation.type];
}
[[byType objectForKey:annotation.type] addObject:annotation];
}
_all = annotations.copy;
_annotationsByType = byType.copy;
}
@end
@implementation ANAnnotation
@dynamic type;
@dynamic value;
- (ANDraftAnnotation *)draftAnnotation {
ANDraftAnnotation * draft = [ANDraftAnnotation new];
draft.type = self.type;
draft.value = self.value;
return draft;
}
@end
#if APPNETKIT_USE_CORE_LOCATION
@implementation ANAnnotation (CLLocation)
- (CLLocationCoordinate2D)geolocationCoordinateValue {
double lat = [[self.value objectForKey:@"latitude"] doubleValue];
double lng = [[self.value objectForKey:@"longitude"] doubleValue];
return CLLocationCoordinate2DMake(lat, lng);
}
- (CLLocationDistance)geolocationAltitudeValue {
NSNumber * alt = [self.value objectForKey:@"altitude"];
if(!alt) {
return 0;
}
return alt.doubleValue;
}
- (CLLocationAccuracy)geolocationHorizontalAccuracyValue {
NSNumber * horiz = [self.value objectForKey:@"horizontal_accuracy"];
if(!horiz) {
return 0;
}
return horiz.doubleValue;
}
- (CLLocationAccuracy)geolocationVerticalAccuracyValue {
if(![self.value objectForKey:@"altitude"]) {
// Return a negative to indicate the corresponding field is invalid.
return -1;
}
NSNumber * vert = [self.value objectForKey:@"vertical_accuracy"];
if(!vert) {
return 0;
}
return vert.doubleValue;
}
- (CLLocation *)geolocationValue {
if(![self.type isEqualToString:ANAnnotationTypeGeolocation]) {
return nil;
}
return [[CLLocation alloc] initWithCoordinate:[self geolocationCoordinateValue] altitude:[self geolocationAltitudeValue] horizontalAccuracy:[self geolocationHorizontalAccuracyValue] verticalAccuracy:[self geolocationVerticalAccuracyValue] timestamp:nil];
}
@end
#endif