-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMapKit+Convenience.m
178 lines (124 loc) · 5.02 KB
/
MapKit+Convenience.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
//
// MapKit+Convenience.m
// Crowd Guard
//
// Created by Alexander Ivanov on 17.11.16.
// Copyright © 2016 Alexander Ivanov. All rights reserved.
//
#import "MapKit+Convenience.h"
@implementation MKMapView (Convenience)
- (BOOL)setRegionWithCenter:(CLLocationCoordinate2D)center distance:(CLLocationDistance)distance animated:(BOOL)animated {
// double latitude = distance;
// double longitude = distance;
// if (self.bounds.size.height > self.bounds.size.width)
// latitude *= self.bounds.size.height / self.bounds.size.width;
// else if (self.bounds.size.height < self.bounds.size.width)
// longitude *= self.bounds.size.width / self.bounds.size.height;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, distance, distance);
if (!MKCoordinateRegionIsValid(region))
return NO;
region = [self regionThatFits:region];
if (!MKCoordinateRegionIsValid(region))
return NO;
[self setRegion:region animated:animated];
return YES;
}
- (void)removeAllAnnotations {
if (self.annotations.count)
[self removeAnnotations:self.annotations];
}
- (void)removeAllOverlays {
if (self.overlays.count)
[self removeOverlays:self.overlays];
}
- (void)removeAllAnnotationsAndOverlays {
[self removeAllAnnotations];
[self removeAllOverlays];
}
- (MKAnnotationView *)viewForNullableAnnotation:(id<MKAnnotation>)annotation {
return annotation ? [self viewForAnnotation:annotation] : Nil;
}
- (void)showAllAnnotations:(BOOL)animated {
[self showAnnotations:self.annotations animated:animated];
}
@end
@implementation MKDistanceFormatter (Convenience)
static MKDistanceFormatter *_defaultFormatter;
+ (instancetype)defaultFormatter {
@synchronized (self) {
if (!_defaultFormatter)
_defaultFormatter = [self new];
}
return _defaultFormatter;
}
@end
@implementation MKShape (Convenience)
- (instancetype)initWithTitle:(NSString *)title subtitle:(NSString *)subtitle {
self = [self init];
if (self) {
self.title = title;
self.subtitle = subtitle;
}
return self;
}
@end
@implementation MKPointAnnotation (Convenience)
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle {
self = [self initWithTitle:title subtitle:subtitle];
if (self)
self.coordinate = coordinate;
return self;
}
@end
@implementation MKDirectionsRequest (Convenience)
- (instancetype)initWithSource:(MKMapItem *)source destination:(MKMapItem *)destination transportType:(MKDirectionsTransportType)transportType requestsAlternateRoutes:(BOOL)requestsAlternateRoutes {
self = [self init];
if (self) {
self.source = source;
self.destination = destination;
self.transportType = transportType;
self.requestsAlternateRoutes = requestsAlternateRoutes;
}
return self;
}
- (instancetype)initWithSourcePlacemark:(MKPlacemark *)source destinationPlacemark:(MKPlacemark *)destination transportType:(MKDirectionsTransportType)transportType requestsAlternateRoutes:(BOOL)requestsAlternateRoutes {
return [self initWithSource:[[MKMapItem alloc] initWithPlacemark:source] destination:[[MKMapItem alloc] initWithPlacemark:destination] transportType:transportType requestsAlternateRoutes:requestsAlternateRoutes];
}
- (instancetype)initWithSourceCoordinate:(CLLocationCoordinate2D)source destinationCoordinate:(CLLocationCoordinate2D)destination transportType:(MKDirectionsTransportType)transportType requestsAlternateRoutes:(BOOL)requestsAlternateRoutes {
return [self initWithSourcePlacemark:[[MKPlacemark alloc] initWithCoordinate:source] destinationPlacemark:[[MKPlacemark alloc] initWithCoordinate:destination] transportType:transportType requestsAlternateRoutes:requestsAlternateRoutes];
}
- (MKDirections *)calculateDirectionsWithCompletionHandler:(MKDirectionsHandler)completionHandler {
MKDirections *directions = [[MKDirections alloc] initWithRequest:self];
[directions calculateDirectionsWithCompletionHandler:completionHandler];
return directions;
}
- (MKDirections *)calculateETAWithCompletionHandler:(MKETAHandler)completionHandler {
MKDirections *directions = [[MKDirections alloc] initWithRequest:self];
[directions calculateETAWithCompletionHandler:completionHandler];
return directions;
}
@end
@implementation MKMapSnapshotOptions (Convenience)
- (instancetype)initWithMapRect:(MKMapRect)mapRect {
self = [self init];
if (self)
self.mapRect = mapRect;
return self;
}
- (instancetype)initWithRegion:(MKCoordinateRegion)region {
self = [self init];
if (self)
self.region = region;
return self;
}
- (MKMapSnapshotter *)snapshotWithCompletionHandler:(MKMapSnapshotCompletionHandler)completionHandler {
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:self];
[snapshotter startWithCompletionHandler:completionHandler];
return snapshotter;
}
- (MKMapSnapshotter *)snapshotWithQueue:(dispatch_queue_t)queue completionHandler:(MKMapSnapshotCompletionHandler)completionHandler {
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:self];
[snapshotter startWithQueue:queue completionHandler:completionHandler];
return snapshotter;
}
@end