/MapView/Map/RMTiledLayerController.m

http://github.com/route-me/route-me · Objective C · 159 lines · 91 code · 32 blank · 36 comment · 5 complexity · e0624cfc54438073e0a6ac63207fd50d MD5 · raw file

  1. //
  2. // RMTiledLayerController.m
  3. //
  4. // Copyright (c) 2008-2009, Route-Me Contributors
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are met:
  9. //
  10. // * Redistributions of source code must retain the above copyright notice, this
  11. // list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above copyright notice,
  13. // this list of conditions and the following disclaimer in the documentation
  14. // and/or other materials provided with the distribution.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. // POSSIBILITY OF SUCH DAMAGE.
  27. #import "RMGlobalConstants.h"
  28. #import "RMTiledLayerController.h"
  29. #import "RMFractalTileProjection.h"
  30. #import "RMTileSource.h"
  31. @implementation RMTiledLayerController
  32. @synthesize layer;
  33. -(id) initWithTileSource: (id <RMTileSource>) _tileSource
  34. {
  35. if (![super init])
  36. return nil;
  37. @throw [NSException exceptionWithName:@"NotImplementedExcption" reason:@"TiledLayerController is not complete. Use CoreAnimationRenderer instead." userInfo:nil];
  38. tileSource = _tileSource;
  39. [tileSource retain];
  40. RMFractalTileProjection *tileProjection = [tileSource tileProjection];
  41. layer = [CATiledLayer layer];
  42. layer.delegate = self;
  43. layer.levelsOfDetail = tileProjection.maxZoom + 1; // check this.
  44. layer.levelsOfDetailBias = 1; // Allows zoom level 0.
  45. layer.tileSize = CGSizeMake(tileProjection.tileSideLength,
  46. tileProjection.tileSideLength);
  47. RMXYRect boundsRect = tileProjection.bounds;
  48. layer.bounds = CGRectMake(boundsRect.origin.x, boundsRect.origin.y, boundsRect.size.width, boundsRect.size.height) ;
  49. layer.position = CGPointZero;
  50. [self setScale:1];
  51. [layer setNeedsDisplay];
  52. return self;
  53. }
  54. -(void) dealloc
  55. {
  56. layer.delegate = nil;
  57. [layer release];
  58. [super dealloc];
  59. }
  60. -(void) setScale: (float) _scale
  61. {
  62. scale = _scale;
  63. layer.transform = CATransform3DMakeScale(1/scale,1/scale, 1.0f);
  64. }
  65. - (float) scale
  66. {
  67. return scale;
  68. }
  69. -(void) centerProjectedPoint: (RMProjectedPoint) aPoint animate: (BOOL) animate
  70. {
  71. if (animate == NO)
  72. {
  73. [CATransaction begin];
  74. [CATransaction setValue:[NSNumber numberWithFloat:0.0f]
  75. forKey:kCATransactionAnimationDuration];
  76. }
  77. // layer.position = CGPointMake(point.x, point.y);
  78. if (animate == NO)
  79. {
  80. [CATransaction commit];
  81. }
  82. // topLeft = point;
  83. // topLeft.x -= viewSize.width * scale / 2;
  84. // topLeft.y -= viewSize.height * scale / 2;
  85. }
  86. -(void) centerLatLong: (CLLocationCoordinate2D) aPoint animate: (BOOL) animate
  87. {
  88. [self centerMercator:[RMMercator toMercator:aPoint] animate: animate];
  89. }
  90. -(void) dragBy: (CGSize) delta
  91. {
  92. [CATransaction begin];
  93. [CATransaction setValue:[NSNumber numberWithFloat:0.0f]
  94. forKey:kCATransactionAnimationDuration];
  95. layer.position = CGPointMake(layer.position.x + delta.width,
  96. layer.position.y + delta.height);
  97. [CATransaction commit];
  98. }
  99. -(void) zoomByFactor: (float) zoomFactor near:(CGPoint) center
  100. {
  101. [self setScale: scale * zoomFactor];
  102. }
  103. - (void)drawLayer:(CALayer *)theLayer
  104. inContext:(CGContextRef)theContext
  105. {
  106. RMLog(@"drawLayer:inContext:");
  107. // CGRect visibleRect = [self visibleRect];
  108. // RMLog(@"visibleRect: %d %d %d %d", visibleRect.origin.x, visibleRect.origin.y, visibleRect.size.width, visibleRect.size.height);
  109. CGRect rect = CGContextGetClipBoundingBox(theContext);
  110. // RMLog(@"rect: %d %d %d %d", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
  111. //CGAffineTransform transform = CGContextGetCTM(theContext);
  112. // RMLog(@"transform scale: a:%f b:%f c:%f d:%f tx:%f ty:%f", transform.a, transform.b, transform.c, transform.d, transform.tx, transform.ty);
  113. /// \bug magic string literals
  114. NSString *path = [[NSBundle mainBundle] pathForResource:@"loading" ofType:@"png"];
  115. CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([path UTF8String]);
  116. CGImageRef image = CGImageCreateWithPNGDataProvider(dataProvider, NULL, FALSE, kCGRenderingIntentDefault);
  117. CGDataProviderRelease(dataProvider);
  118. CGContextDrawImage(theContext, rect, image);
  119. }
  120. - (void)setFrame:(CGRect)frame
  121. {
  122. }
  123. - (CALayer*) layer
  124. {
  125. return layer;
  126. }
  127. @end