/
OPEMapManager.m
426 lines (340 loc) · 14 KB
/
OPEMapManager.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//
// OPEMapManager.m
// OSM POI Editor
//
// Created by David on 9/24/13.
//
//
#import "OPEMapManager.h"
#import "RMMapViewDelegate.h"
#import "RMMarker.h"
#import "RMMapView.h"
#import "RMAnnotation.h"
#import "RMPointAnnotation.h"
#import "RMShape.h"
#import "OPEOsmElement.h"
#import "OPEOsmNode.h"
#import "OPEOsmWay.h"
#import "OPEOsmRelation.h"
#import "OSMNote.h"
#import "OPEOSMData.h"
#import "OPEGeoCentroid.h"
#import "OPENotesDatabase.h"
#import "OPELog.h"
@interface OPEMapManager ()
@property (nonatomic, strong) NSMutableDictionary * imageDictionary;
@property (nonatomic, strong) RMAnnotation * wayAnnotation;
@property (nonatomic, strong) NSOperationQueue * operationQueue;
@end
@implementation OPEMapManager
-(id)init
{
if(self = [super init])
{
self.imageDictionary = [NSMutableDictionary dictionary];
self.operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue.maxConcurrentOperationCount = 2;
}
return self;
}
-(OPEOSMData *)osmData
{
if(!_osmData) {
_osmData = [[OPEOSMData alloc] init];
}
return _osmData;
}
//Create Bordered image for annotation
- (UIImage*)imageWithBorderFromImage:(UIImage*)source //Draw box around centered image
{
CGSize imgSize = [source size];
float rectSize;
if (imgSize.width > imgSize.height) {
rectSize = imgSize.width;
}
else {
rectSize = imgSize.height;
}
UIView * view;
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rectSize+4,rectSize+4)];
UIImageView * imageView = [[UIImageView alloc] initWithImage:source];
[view addSubview:imageView];
[view sizeToFit];
imageView.center = view.center; //Center the Image
[view.layer setBorderColor: [[UIColor blackColor] CGColor]];
[view.layer setBorderWidth: 1.0];
[view setBackgroundColor:[UIColor whiteColor]];
CGSize size = [view bounds].size;
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
[[view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
-(RMMarker *)markerWithManagedObject:(OPEOsmElement *)managedOsmElement
{
UIImage * icon = nil;
if (managedOsmElement.type) {
if ([self.imageDictionary objectForKey:managedOsmElement.type.imageString]) {
icon = [self.imageDictionary objectForKey:managedOsmElement.type.imageString];
}
else {
NSString * imageString = managedOsmElement.type.imageString;
if(![UIImage imageNamed:imageString])
imageString = @"none.png";
icon = [self imageWithBorderFromImage:[UIImage imageNamed:imageString]]; //center image inside box
if(icon && managedOsmElement.type.imageString)
{
[self.imageDictionary setObject:icon forKey:managedOsmElement.type.imageString];
}
else {
DDLogWarn(@"WARN: No Icon");
}
}
}
RMMarker *newMarker = [[RMMarker alloc] initWithUIImage:icon anchorPoint:CGPointMake(0.5, 0.5)];
newMarker.userInfo = managedOsmElement;
newMarker.zPosition = 0.2;
return newMarker;
}
-(RMAnnotation *)annotationWForNote:(OSMNote *)note withMapView:(RMMapView *)mapView
{
RMAnnotation * annotation = [RMAnnotation annotationWithMapView:mapView coordinate:note.coordinate andTitle:@"Note"];
annotation.userInfo = note;
return annotation;
}
-(NSArray *)annotationsForOsmElement:(OPEOsmElement *)managedOsmElement withMapView:(RMMapView *)mapView
{
[self.osmData getTypeFor:managedOsmElement];
RMAnnotation * annotation = [[RMAnnotation alloc] initWithMapView:mapView coordinate:[self.osmData centerForElement:managedOsmElement] andTitle:[self.osmData nameForElement:managedOsmElement]];
NSMutableString * subtitleString = [NSMutableString stringWithFormat:@"%@",managedOsmElement.type.categoryName];
if ([[managedOsmElement valueForOsmKey:@"name"] length]) {
[subtitleString appendFormat:@" - %@",managedOsmElement.type.name];
}
annotation.subtitle = subtitleString;
annotation.userInfo = managedOsmElement;
if ([managedOsmElement isKindOfClass:[OPEOsmRelation class]]) {
OPEOsmRelation * managedRelation = (OPEOsmRelation *)managedOsmElement;
NSArray * outerPolygonArray = [self.osmData outerPolygonsForRelation:managedRelation];
NSMutableArray * annotationsArray = [NSMutableArray array];
for (NSArray * pointsArray in outerPolygonArray)
{
CLLocationCoordinate2D center = [[[OPEGeoCentroid alloc] init] centroidOfPolygon:pointsArray];
RMAnnotation * newAnnoation = [RMAnnotation annotationWithMapView:mapView coordinate:center andTitle:annotation.title];
newAnnoation.subtitle = annotation.subtitle;
newAnnoation.userInfo = annotation.userInfo;
//set center for each outer;
[annotationsArray addObject:newAnnoation];
}
if ([annotationsArray count]) {
return annotationsArray;
}
}
return @[annotation];
}
-(RMAnnotation *)shapeForRelation:(OPEOsmRelation *)relation withMapView:(RMMapView *)mapView
{
NSArray * outerPoints = [self.osmData outerPolygonsForRelation:relation];
NSArray * innerPoints = [self.osmData innerPolygonsForRelation:relation];
if (![outerPoints count]) {
return nil;
}
RMAnnotation * newAnnotation = [[RMAnnotation alloc] initWithMapView:mapView coordinate:((CLLocation *)[[outerPoints objectAtIndex:0] objectAtIndex:0]).coordinate andTitle:nil];
RMShape *shape = [[RMShape alloc] initWithView:mapView];
[shape performBatchOperations:^(RMShape *aShape)
{
for (NSArray * points in outerPoints)
{
[aShape moveToCoordinate:((CLLocation *)[points objectAtIndex:0]).coordinate];
for (CLLocation *point in points)
{
[aShape addLineToCoordinate:point.coordinate];
}
//[aShape closePath];
}
if ([innerPoints count])
{
[aShape moveToCoordinate:((CLLocation *)[[innerPoints objectAtIndex:0] objectAtIndex:0]).coordinate];
for (NSArray * points in innerPoints)
{
[aShape moveToCoordinate:((CLLocation *)[points objectAtIndex:0]).coordinate];
for (CLLocation *point in points)
{
[aShape addLineToCoordinate:point.coordinate];
}
//[aShape closePath];
}
}
aShape.lineColor = [UIColor blackColor];
aShape.lineWidth +=1;
aShape.fillColor = [UIColor colorWithWhite:.5 alpha:.6];
aShape.fillRule = kCAFillRuleEvenOdd;
}];
newAnnotation.layer = shape;
return newAnnotation;
}
-(RMAnnotation *)shapeForWay:(OPEOsmWay *)way withMapView:(RMMapView *)mapView
{
NSArray * points = [self.osmData pointsForWay:way];
BOOL isArea = [self.osmData isArea:way];
RMAnnotation * newAnnotation = [[RMAnnotation alloc] initWithMapView:mapView coordinate:((CLLocation *)[points objectAtIndex:0]).coordinate andTitle:nil];
RMShape * shape = [[RMShape alloc]initWithView:mapView];
[shape performBatchOperations:^(RMShape *aShape) {
[aShape moveToCoordinate:((CLLocation *)[points objectAtIndex:0]).coordinate];
for (CLLocation *point in points)
[aShape addLineToCoordinate:point.coordinate];
if (isArea) {
[aShape closePath];
}
}];
shape.lineColor = [UIColor blackColor];
shape.lineWidth +=1;
if (isArea) {
shape.fillColor = [UIColor colorWithWhite:.5 alpha:.6];
}
else
{
shape.fillColor = [UIColor clearColor];
}
newAnnotation.layer = shape;
return newAnnotation;
}
//////////////////////////////////
#pragma mark AnnotationManagement
//////////////////////////////////
- (void)reloadNotesInMapView:(RMMapView *)mapView
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
__block NSMutableArray *annotationsToBeRemoved = [NSMutableArray array];
[mapView.annotations enumerateObjectsUsingBlock:^(RMAnnotation *annotation, NSUInteger idx, BOOL *stop) {
if ([annotation.userInfo isKindOfClass:[OSMNote class]]) {
[annotationsToBeRemoved addObject:annotation];
}
}];
[OPENotesDatabase allNotesCompletion:^(NSArray *notes) {
[mapView removeAnnotations:annotationsToBeRemoved];
[self addNotes:notes withMapView:mapView];
}];
});
}
- (RMAnnotation *)existingAnnotationForNote:(OSMNote *)note withMapView:(RMMapView *)mapView
{
__block RMAnnotation *noteAnnotation = nil;
[mapView.annotations enumerateObjectsUsingBlock:^(RMAnnotation *annotation, NSUInteger idx, BOOL *stop) {
if ([annotation.userInfo isKindOfClass:[OSMNote class]]) {
OSMNote *annotationNote = (OSMNote *)annotation.userInfo;
if (annotationNote.id == note.id) {
noteAnnotation = annotation;
*stop = YES;
}
}
}];
return noteAnnotation;
}
- (void)addNote:(OSMNote *)note withMapView:(RMMapView *)mapView
{
RMAnnotation *oldAnnotation = [self existingAnnotationForNote:note withMapView:mapView];
if (oldAnnotation) {
[mapView removeAnnotation:oldAnnotation];
}
RMAnnotation * annotation = [self annotationWForNote:note withMapView:mapView];
[mapView addAnnotation:annotation];
}
- (void)addNotes:(NSArray *)notes withMapView:(RMMapView *)mapView
{
[notes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[self addNote:obj withMapView:mapView];
}];
}
- (void)addAnnotationsForOsmElements:(NSArray *)elementsArray withMapView:(RMMapView *)mapView {
[elementsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSArray * annotationsArray = [self annotationsForOsmElement:obj withMapView:mapView];
[annotationsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[mapView addAnnotation:obj];
}];
}];
}
- (void)updateAnnotationsForOsmElements:(NSArray *)elementsArray withMapView:(RMMapView *)mapView {
for (OPEOsmElement * element in elementsArray)
{
[self removeAnnotationForOsmElement:element withMapView:mapView];
if (![element.action isEqualToString:kActionTypeDelete]) {
NSArray * annotationsArray = [self annotationsForOsmElement:element withMapView:mapView];
for(RMAnnotation * annotation in annotationsArray) {
[mapView addAnnotation:annotation];
}
}
}
}
- (void)removeAnnotationForOsmElement:(OPEOsmElement *)element withMapView:(RMMapView *)mapView
{
NSSet * annotationSet = [self existingAnnotationsForOsmElement:element withMapView:mapView];
if ([annotationSet count])
{
[mapView removeAnnotations:[annotationSet allObjects]];
}
}
- (NSSet *)existingAnnotationsForOsmElement:(OPEOsmElement *)element withMapView:(RMMapView *)mapView {
NSIndexSet * indexSet = [self indexesOfOsmElement:element withMapView:mapView];
NSMutableSet * annotationSet = [NSMutableSet set];
[indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
[annotationSet addObject:[mapView.annotations objectAtIndex:idx]];
}];
return annotationSet;
}
- (NSIndexSet *)indexesOfOsmElement:(OPEOsmElement *)element withMapView:(RMMapView *)mapView
{
NSIndexSet * set = [mapView.annotations indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
RMAnnotation * annotation = (RMAnnotation *)obj;
return [annotation.userInfo isEqual:element];
}];
return set;
}
/////////////////////////////////
#pragma mark RMMapViewDelegate
/////////////////////////////////
- (RMMapLayer *) mapView:(RMMapView *)mView layerForAnnotation:(RMAnnotation *)annotation
{
RMMarker * marker = nil;
if ([annotation.userInfo isKindOfClass:[OPEOsmElement class]]) {
OPEOsmElement * managedOsmElement = (OPEOsmElement *)annotation.userInfo;;
marker = [self markerWithManagedObject:managedOsmElement];
marker.canShowCallout = YES;
marker.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
else if ([annotation.userInfo isKindOfClass:[OSMNote class]]) {
OSMNote * note = annotation.userInfo;
UIImage * image = nil;
if (note.isOpen) {
image = [UIImage imageNamed:@"note_open.png"];
}
else {
image = [UIImage imageNamed:@"note_closed.png"];
}
marker = [[RMMarker alloc] initWithUIImage:image anchorPoint:CGPointMake(0.5, 0.5)];
marker.canShowCallout = NO;
}
return marker;
}
- (void)tapOnAnnotation:(RMAnnotation *)annotation onMap:(RMMapView *)mapView
{
if (self.wayAnnotation) {
[mapView removeAnnotation:self.wayAnnotation];
self.wayAnnotation = nil;
}
id osmElement = annotation.userInfo;
if ([osmElement isKindOfClass:[OPEOsmWay class]]) {
OPEOsmWay * osmWay = (OPEOsmWay *)osmElement;
self.wayAnnotation = [self shapeForWay:osmWay withMapView:mapView];
self.wayAnnotation.userInfo = annotation.userInfo;
[mapView addAnnotation:self.wayAnnotation];
}
else if ([osmElement isKindOfClass:[OPEOsmRelation class]])
{
OPEOsmRelation * osmRelation = (OPEOsmRelation *)osmElement;
self.wayAnnotation = [self shapeForRelation:osmRelation withMapView:mapView];
self.wayAnnotation.userInfo = annotation.userInfo;
[mapView addAnnotation:self.wayAnnotation];
}
}
@end