/MapView/Map/RMVirtualEarthSource.m

http://github.com/route-me/route-me · Objective C · 143 lines · 101 code · 15 blank · 27 comment · 9 complexity · 8a45022da0818614dc8b3eee04e6aa4f MD5 · raw file

  1. //
  2. // RMVirtualEarthURL.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 "RMVirtualEarthSource.h"
  28. @implementation RMVirtualEarthSource
  29. - (id) init
  30. {
  31. return [self initWithHybridThemeUsingAccessKey:@""];
  32. }
  33. - (id) initWithAerialThemeUsingAccessKey:(NSString *)developerAccessKey
  34. {
  35. RMLog(@"please see comments in RMVirtualEarthSource.h");
  36. NSAssert(([developerAccessKey length] > 0), @"Virtual Earth access key must be non-empty");
  37. if (self = [super init])
  38. {
  39. [self setMaxZoom:18];
  40. [self setMinZoom:1];
  41. maptypeFlag = @"a";
  42. accessKey = developerAccessKey;
  43. _shortName = @"Microsoft Virtual Earth satellite";
  44. }
  45. return self;
  46. }
  47. - (id) initWithRoadThemeUsingAccessKey:(NSString *)developerAccessKey
  48. {
  49. RMLog(@"please see comments in RMVirtualEarthSource.h");
  50. NSAssert(([developerAccessKey length] > 0), @"Virtual Earth access key must be non-empty");
  51. if (self = [super init])
  52. {
  53. [self setMaxZoom:18];
  54. [self setMinZoom:1];
  55. maptypeFlag = @"r";
  56. accessKey = developerAccessKey;
  57. _shortName = @"Microsoft Virtual Earth roads";
  58. }
  59. return self;
  60. }
  61. - (id) initWithHybridThemeUsingAccessKey:(NSString *)developerAccessKey
  62. {
  63. RMLog(@"please see comments in RMVirtualEarthSource.h");
  64. NSAssert(([developerAccessKey length] > 0), @"Virtual Earth access key must be non-empty");
  65. if (self = [super init])
  66. {
  67. [self setMaxZoom:18];
  68. [self setMinZoom:1];
  69. maptypeFlag = @"h";
  70. accessKey = developerAccessKey;
  71. _shortName = @"Microsoft Virtual Earth hybrid";
  72. }
  73. return self;
  74. }
  75. -(NSString*) tileURL: (RMTile) tile
  76. {
  77. NSString *quadKey = [self quadKeyForTile:tile];
  78. return [self urlForQuadKey:quadKey];
  79. }
  80. -(NSString*) quadKeyForTile: (RMTile) tile
  81. {
  82. NSAssert4(((tile.zoom >= self.minZoom) && (tile.zoom <= self.maxZoom)),
  83. @"%@ tried to retrieve tile with zoomLevel %d, outside source's defined range %f to %f",
  84. self, tile.zoom, self.minZoom, self.maxZoom);
  85. NSMutableString *quadKey = [NSMutableString string];
  86. for (int i = tile.zoom; i > 0; i--)
  87. {
  88. int mask = 1 << (i - 1);
  89. int cell = 0;
  90. if ((tile.x & mask) != 0)
  91. {
  92. cell++;
  93. }
  94. if ((tile.y & mask) != 0)
  95. {
  96. cell += 2;
  97. }
  98. [quadKey appendString:[NSString stringWithFormat:@"%d", cell]];
  99. }
  100. return quadKey;
  101. }
  102. -(NSString*) urlForQuadKey: (NSString*) quadKey
  103. {
  104. NSString *mapExtension = @".png"; //extension
  105. //TODO what is the ?g= hanging off the end 1 or 15?
  106. return [NSString stringWithFormat:@"http://%@%d.ortho.tiles.virtualearth.net/tiles/%@%@%@?g=15", maptypeFlag, 3, maptypeFlag, quadKey, mapExtension];
  107. }
  108. -(NSString*) uniqueTilecacheKey
  109. {
  110. return [NSString stringWithFormat:@"MicrosoftVirtualEarth%@", maptypeFlag];
  111. }
  112. -(NSString *)shortName
  113. {
  114. return _shortName;
  115. }
  116. -(NSString *)longDescription
  117. {
  118. return @"Microsoft Virtual Earth. All data © Microsoft or their licensees.";
  119. }
  120. -(NSString *)shortAttribution
  121. {
  122. return @"© Microsoft Virtual Earth";
  123. }
  124. -(NSString *)longAttribution
  125. {
  126. return @"Map data © Microsoft Virtual Earth.";
  127. }
  128. @end