RMMapContents.m
11.9 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//
// RMMapContents.m
// MapView
//
// Created by Joseph Gentle on 24/09/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "RMMapContents.h"
#import "RMFoundation.h"
#import "RMProjection.h"
#import "RMMercatorToScreenProjection.h"
#import "RMMercatorToTileProjection.h"
#import "RMTileSource.h"
#import "RMTileLoader.h"
#import "RMTileImageSet.h"
#import "RMOpenStreetMapsSource.h"
#import "RMCoreAnimationRenderer.h"
#import "RMCachedTileSource.h"
#import "RMLayerSet.h"
#import "RMMarker.h"
//#import "RMCloudMadeMapSource.h"
@implementation RMMapContents
#pragma mark Initialisation
- (id) initForView: (UIView*) view
{
// id<RMTileSource> _tileSource = [[RMCloudMadeMapSource alloc] init];
id<RMTileSource> _tileSource = [[RMOpenStreetMapsSource alloc] init];
RMMapRenderer *_renderer = [[RMCoreAnimationRenderer alloc] initWithContent:self];
CLLocationCoordinate2D here;
here.latitude = -33.9464;
here.longitude = 151.2381;
// here.latitude = 65.146;
// here.longitude = 189.9;
id mapContents = [self initForView:view WithTileSource:_tileSource WithRenderer:_renderer LookingAt:here];
[_tileSource release];
[_renderer release];
return mapContents;
}
- (id) initForView: (UIView*) view WithTileSource: (id<RMTileSource>)_tileSource WithRenderer: (RMMapRenderer*)_renderer LookingAt:(CLLocationCoordinate2D)latlong
{
if (![super init])
return nil;
// targetView = view;
mercatorToScreenProjection = [[RMMercatorToScreenProjection alloc] initWithScreenBounds:[view bounds]];
tileSource = nil;
projection = nil;
mercatorToTileProjection = nil;
renderer = nil;
imagesOnScreen = nil;
tileLoader = nil;
layer = [[view layer] retain];
[self setTileSource:_tileSource];
[self setRenderer:_renderer];
imagesOnScreen = [[RMTileImageSet alloc] initWithDelegate:renderer];
[imagesOnScreen setTileSource:[RMCachedTileSource cachedTileSourceWithSource:tileSource]];
tileLoader = [[RMTileLoader alloc] initWithContent:self];
[tileLoader setSuppressLoading:YES];
[self setZoom:15];
[self moveToLatLong:latlong];
[tileLoader setSuppressLoading:NO];
// TODO: Make a nice background class
RMMapLayer *theBackground = [[RMMapLayer alloc] init];
[self setBackground:theBackground];
[theBackground release];
RMLayerSet *theOverlay = [[RMLayerSet alloc] initForContents:self];
[self setOverlay:theOverlay];
[theOverlay release];
[view setNeedsDisplay];
NSLog(@"Map contents initialised. view: %@ tileSource %@ renderer %@", view, tileSource, renderer);
return self;
}
-(void) dealloc
{
[renderer release];
[tileSource release];
[tileLoader release];
[projection release];
[mercatorToTileProjection release];
[mercatorToScreenProjection release];
[tileSource release];
[layer release];
[super dealloc];
}
#pragma mark Forwarded Events
- (void)moveToLatLong: (CLLocationCoordinate2D)latlong
{
RMXYPoint aPoint = [projection latLongToPoint:latlong];
[self moveToXYPoint: aPoint];
}
- (void)moveToXYPoint: (RMXYPoint)aPoint
{
[mercatorToScreenProjection setXYCenter:aPoint];
// [imagesOnScreen removeAllTiles];
[tileLoader clearLoadedBounds];
[tileLoader updateLoadedImages];
[renderer setNeedsDisplay];
}
- (void)moveBy: (CGSize) delta
{
[mercatorToScreenProjection moveScreenBy:delta];
[imagesOnScreen moveBy:delta];
[tileLoader moveBy:delta];
[overlay moveBy:delta];
[renderer setNeedsDisplay];
}
- (void)zoomByFactor: (float) zoomFactor near:(CGPoint) pivot
{
[mercatorToScreenProjection zoomScreenByFactor:zoomFactor near:pivot];
[imagesOnScreen zoomByFactor:zoomFactor near:pivot];
[tileLoader zoomByFactor:zoomFactor near:pivot];
[overlay zoomByFactor:zoomFactor near:pivot];
[renderer setNeedsDisplay];
}
- (void) drawRect: (CGRect) aRect
{
[renderer drawRect:aRect];
}
#pragma mark Properties
- (void) setTileSource: (id<RMTileSource>)newTileSource
{
[tileSource release];
tileSource = [newTileSource retain];
[projection release];
projection = [[tileSource projection] retain];
[mercatorToTileProjection release];
mercatorToTileProjection = [[tileSource mercatorToTileProjection] retain];
[imagesOnScreen setTileSource:tileSource];
}
- (id<RMTileSource>) tileSource
{
return [[tileSource retain] autorelease];
}
- (void) setRenderer: (RMMapRenderer*) newRenderer
{
if (renderer == newRenderer)
return;
[[renderer layer] removeFromSuperlayer];
[renderer release];
renderer = [newRenderer retain];
// CGRect rect = [self screenBounds];
// NSLog(@"%f %f %f %f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
[[renderer layer] setFrame:[self screenBounds]];
if (background != nil)
[layer insertSublayer:[renderer layer] above:background];
else if (overlay != nil)
[layer insertSublayer:[renderer layer] below:overlay];
else
[layer addSublayer:[renderer layer]];
}
- (RMMapRenderer *)renderer
{
return [[renderer retain] autorelease];
}
- (void) setBackground: (RMMapLayer*) aLayer
{
if (background != nil)
{
[background release];
[background removeFromSuperlayer];
}
background = [aLayer retain];
background.frame = [self screenBounds];
if ([renderer layer] != nil)
[layer insertSublayer:background below:[renderer layer]];
else if (overlay != nil)
[layer insertSublayer:background below:overlay];
else
[layer addSublayer:background];
}
- (RMMapLayer *)background
{
return [[background retain] autorelease];
}
- (void) setOverlay: (RMLayerSet*) aLayer
{
if (overlay != nil)
{
[overlay release];
[overlay removeFromSuperlayer];
}
overlay = [aLayer retain];
overlay.frame = [self screenBounds];
if ([renderer layer] != nil)
[layer insertSublayer:overlay above:[renderer layer]];
else if (background != nil)
[layer insertSublayer:overlay above:background];
else
[layer addSublayer:overlay];
/* Test to make sure the overlay is working.
CALayer *testLayer = [[CALayer alloc] init];
[testLayer setFrame:CGRectMake(100, 100, 200, 200)];
[testLayer setBackgroundColor:[[UIColor brownColor] CGColor]];
NSLog(@"added test layer");
[overlay addSublayer:testLayer];*/
}
- (RMLayerSet *)overlay
{
return [[overlay retain] autorelease];
}
- (CLLocationCoordinate2D) mapCenter
{
RMXYPoint aPoint = [mercatorToScreenProjection XYCenter];
return [projection pointToLatLong:aPoint];
}
-(void) setMapCenter: (CLLocationCoordinate2D) center
{
[self moveToLatLong:center];
}
-(RMXYRect) XYBounds
{
return [mercatorToScreenProjection XYBounds];
}
-(void) setXYBounds: (RMXYRect) boundsRect
{
[mercatorToScreenProjection setXYBounds:boundsRect];
}
-(RMTileRect) tileBounds
{
return [mercatorToTileProjection project: mercatorToScreenProjection];
}
-(CGRect) screenBounds
{
if (mercatorToScreenProjection != nil)
return [mercatorToScreenProjection screenBounds];
else
return CGRectMake(0, 0, 0, 0);
}
-(float) scale
{
return [mercatorToScreenProjection scale];
}
-(void) setScale: (float) scale
{
[mercatorToScreenProjection setScale:scale];
[tileLoader updateLoadedImages];
[renderer setNeedsDisplay];
}
-(float) zoom
{
return [mercatorToTileProjection calculateZoomFromScale:[mercatorToScreenProjection scale]];
}
-(void) setZoom: (float) zoom
{
float scale = [mercatorToTileProjection calculateScaleFromZoom:zoom];
[self setScale:scale];
}
-(RMTileImageSet*) imagesOnScreen
{
return [[imagesOnScreen retain] autorelease];
}
-(RMProjection*) projection
{
return [[projection retain] autorelease];
}
-(id<RMMercatorToTileProjection>) mercatorToTileProjection
{
return [[mercatorToTileProjection retain] autorelease];
}
-(RMMercatorToScreenProjection*) mercatorToScreenProjection
{
return [[mercatorToScreenProjection retain] autorelease];
}
- (CALayer *)layer
{
return [[layer retain] autorelease];
}
static BOOL _performExpensiveOperations = YES;
+ (BOOL) performExpensiveOperations
{
return _performExpensiveOperations;
}
+ (void) setPerformExpensiveOperations: (BOOL)p
{
if (p == _performExpensiveOperations)
return;
_performExpensiveOperations = p;
if (p)
[[NSNotificationCenter defaultCenter] postNotificationName:RMResumeExpensiveOperations object:self];
else
[[NSNotificationCenter defaultCenter] postNotificationName:RMSuspendExpensiveOperations object:self];
}
#pragma mark LatLng/Pixel translation functions
- (CGPoint)latLongToPixel:(CLLocationCoordinate2D)latlong
{
return [mercatorToScreenProjection projectXYPoint:[projection latLongToPoint:latlong]];
}
- (CLLocationCoordinate2D)pixelToLatLong:(CGPoint)pixel
{
return [projection pointToLatLong:[mercatorToScreenProjection projectScreenPointToXY:pixel]];
}
#pragma mark Zoom With Bounds
- (void)zoomWithLatLngBoundsNorthEast:(CLLocationCoordinate2D)ne SouthWest:(CLLocationCoordinate2D)sw
{
if(ne.latitude == sw.latitude && ne.longitude == sw.longitude)//There are no bounds, probably only one marker.
{
RMXYRect zoomRect;
RMXYPoint myOrigin = [projection latLongToPoint:sw];
//Default is with scale = 2.0 mercators/pixel
zoomRect.size.width = [self screenBounds].size.width * 2.0;
zoomRect.size.height = [self screenBounds].size.height * 2.0;
myOrigin.x = myOrigin.x - (zoomRect.size.width / 2);
myOrigin.y = myOrigin.y - (zoomRect.size.height / 2);
zoomRect.origin = myOrigin;
[self zoomWithRMMercatorRectBounds:zoomRect];
}
else
{
//convert ne/sw into RMMercatorRect and call zoomWithBounds
float pixelBuffer = 50;
CLLocationCoordinate2D latLngBounds;
latLngBounds.longitude = ne.longitude - sw.longitude;
latLngBounds.latitude = ne.latitude - sw.latitude;
CLLocationCoordinate2D midpoint;
midpoint.latitude = (ne.latitude + sw.latitude) / 2;
midpoint.longitude = (ne.longitude + sw.longitude) / 2;
RMXYPoint myOrigin = [projection latLongToPoint:midpoint];
RMXYPoint myPoint = [projection latLongToPoint:latLngBounds];
//Create the new zoom layout
RMXYRect zoomRect;
//Default is with scale = 2.0 mercators/pixel
zoomRect.size.width = [self screenBounds].size.width * 2.0;
zoomRect.size.height = [self screenBounds].size.height * 2.0;
if((myPoint.x / ([self screenBounds].size.width)) < (myPoint.y / ([self screenBounds].size.height)))
{
if((myPoint.y / ([self screenBounds].size.height - pixelBuffer)) > 1)
{
zoomRect.size.width = [self screenBounds].size.width * (myPoint.y / ([self screenBounds].size.height - pixelBuffer));
zoomRect.size.height = [self screenBounds].size.height * (myPoint.y / ([self screenBounds].size.height - pixelBuffer));
}
}
else
{
if((myPoint.x / ([self screenBounds].size.width - pixelBuffer)) > 1)
{
zoomRect.size.width = [self screenBounds].size.width * (myPoint.x / ([self screenBounds].size.width - pixelBuffer));
zoomRect.size.height = [self screenBounds].size.height * (myPoint.x / ([self screenBounds].size.width - pixelBuffer));
}
}
myOrigin.x = myOrigin.x - (zoomRect.size.width / 2);
myOrigin.y = myOrigin.y - (zoomRect.size.height / 2);
NSLog(@"Origin is calculated at: %f, %f", [projection pointToLatLong:myOrigin].latitude, [projection pointToLatLong:myOrigin].longitude);
/*It gets all messed up if our origin is lower than the lowest place on the map, so we check.
if(myOrigin.y < -19971868.880409)
{
myOrigin.y = -19971868.880409;
}*/
zoomRect.origin = myOrigin;
[self zoomWithRMMercatorRectBounds:zoomRect];
}
}
- (void)zoomWithRMMercatorRectBounds:(RMXYRect)bounds
{
[self setXYBounds:bounds];
[overlay correctPositionOfAllSublayers];
[tileLoader clearLoadedBounds];
[tileLoader updateLoadedImages];
[renderer setNeedsDisplay];
}
#pragma mark Markers and overlays
// Move overlays stuff here - at the moment overlay stuff is above...
- (void) addMarker: (RMMarker*)marker
{
[overlay addSublayer:marker];
}
- (void) addMarker: (RMMarker*)marker AtLatLong:(CLLocationCoordinate2D)point
{
[marker setLocation:[projection latLongToPoint:point]];
[self addMarker: marker];
}
- (void) addDefaultMarkerAt: (CLLocationCoordinate2D)point
{
RMMarker *marker = [[RMMarker alloc] initWithKey:RMMarkerRedKey];
[self addMarker:marker AtLatLong:point];
[marker release];
}
- (void) removeMarkers
{
overlay.sublayers = [NSArray arrayWithObjects:nil];
}
@end