diff options
author | Jesse Bounds <jesse@rebounds.net> | 2016-09-22 17:30:47 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-22 17:30:47 -0700 |
commit | 3ddb8dfc86753120d2ea2c3befdf2430c1c56562 (patch) | |
tree | bc241d8c66ea7f78ae4728f9644673e8994e1295 /platform/darwin | |
parent | 08c08f6705d168f0bc7cde251cdacb2c4a53bb7a (diff) | |
download | qtlocation-mapboxgl-3ddb8dfc86753120d2ea2c3befdf2430c1c56562.tar.gz |
[ios, macos] Add tile template option to raster and vector sources
A new MGLTileSet class that wraps all available core Tileset options can be created and passed into the sources. The sources pass that tileset object along to core.
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/src/MGLRasterSource.h | 12 | ||||
-rw-r--r-- | platform/darwin/src/MGLRasterSource.mm | 37 | ||||
-rw-r--r-- | platform/darwin/src/MGLTileSet.h | 78 | ||||
-rw-r--r-- | platform/darwin/src/MGLTileSet.mm | 87 | ||||
-rw-r--r-- | platform/darwin/src/MGLTileSet_Private.h | 9 | ||||
-rw-r--r-- | platform/darwin/src/MGLVectorSource.h | 10 | ||||
-rw-r--r-- | platform/darwin/src/MGLVectorSource.mm | 31 |
7 files changed, 255 insertions, 9 deletions
diff --git a/platform/darwin/src/MGLRasterSource.h b/platform/darwin/src/MGLRasterSource.h index e563244bce..3b22257cb6 100644 --- a/platform/darwin/src/MGLRasterSource.h +++ b/platform/darwin/src/MGLRasterSource.h @@ -1,10 +1,20 @@ #import "MGLSource.h" +#import "MGLTypes.h" + +@class MGLTileSet; + +NS_ASSUME_NONNULL_BEGIN @interface MGLRasterSource : MGLSource @property (nonatomic, readonly, copy) NSURL *URL; -@property (nonatomic, readonly, assign) CGFloat tileSize; +@property (nonatomic, readonly, assign) NSUInteger tileSize; +@property (nonatomic, readonly, nullable) MGLTileSet *tileSet; - (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url tileSize:(CGFloat)tileSize; +- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier tileSize:(CGFloat)tileSize tileSet:(MGLTileSet *)tileSet; + @end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLRasterSource.mm b/platform/darwin/src/MGLRasterSource.mm index 15ca2fc558..06d5a9689c 100644 --- a/platform/darwin/src/MGLRasterSource.mm +++ b/platform/darwin/src/MGLRasterSource.mm @@ -1,12 +1,15 @@ #import "MGLRasterSource.h" #import "MGLSource_Private.h" +#import "MGLTileSet_Private.h" +#import "NSURL+MGLAdditions.h" #include <mbgl/style/sources/raster_source.hpp> @implementation MGLRasterSource -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url tileSize:(CGFloat)tileSize { +- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url tileSize:(CGFloat)tileSize +{ if (self = [super initWithSourceIdentifier:sourceIdentifier]) { _URL = url; _tileSize = tileSize; @@ -14,10 +17,34 @@ return self; } -- (std::unique_ptr<mbgl::style::Source>)mbglSource { - auto source = std::make_unique<mbgl::style::RasterSource>(self.sourceIdentifier.UTF8String, - self.URL.absoluteString.UTF8String, - uint16_t(self.tileSize)); +- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier tileSize:(CGFloat)tileSize tileSet:(MGLTileSet *)tileSet; +{ + if (self = [super initWithSourceIdentifier:sourceIdentifier]) + { + _tileSize = tileSize; + _tileSet = tileSet; + } + return self; +} + +- (std::unique_ptr<mbgl::style::Source>)mbglSource +{ + std::unique_ptr<mbgl::style::RasterSource> source; + + if (self.URL) + { + source = std::make_unique<mbgl::style::RasterSource>(self.sourceIdentifier.UTF8String, + self.URL.mgl_URLByStandardizingScheme.absoluteString.UTF8String, + uint16_t(self.tileSize)); + } + else + { + source = std::make_unique<mbgl::style::RasterSource>(self.sourceIdentifier.UTF8String, + self.tileSet.mbglTileset, + uint16_t(self.tileSize)); + + } + return std::move(source); } diff --git a/platform/darwin/src/MGLTileSet.h b/platform/darwin/src/MGLTileSet.h new file mode 100644 index 0000000000..5ae899d833 --- /dev/null +++ b/platform/darwin/src/MGLTileSet.h @@ -0,0 +1,78 @@ +#import <Foundation/Foundation.h> +#import "MGLTypes.h" + +NS_ASSUME_NONNULL_BEGIN + +/** These constants represent the scheme that the tile URL templates will use. */ +typedef NS_ENUM(NSUInteger, MGLTileSetScheme) { + MGLTileSetSchemeXYZ = 0, + MGLTileSetSchemeTMS +}; + +/** + The `MGLTileSet` class holds the tile URL template strings and associated + configuration for those strings. It can be passed to an `MGLVectorSource` or + `MGLRasterSource` instead of an `NSURL` representing a TileJSON URL to create a + source. + */ +@interface MGLTileSet : NSObject + +/** + An `NSArray` of `NSString` objects that represent the tile templates. + */ +@property (nonatomic, copy) NS_ARRAY_OF(NSString *) *tileURLTemplates; + +/** + An `NSNumber` object containing an integer; specifies the minimum zoom level at + which the source will display tiles. The value should be in the range of 0 to + 22. The default value is 0 and the source will use the default value + if `minimumZoomLevel` is nil. + */ +@property (nonatomic, nullable) NSNumber *minimumZoomLevel; + +/** + An `NSNumber` object containing an integer; specifies the maximum zoom level at + which to display tiles. The value should be in the range of 0 to 22 and greater + than `minimumZoomLevel`. The default value is 22 and the source will use the + default value if `maximumZoomLevel` is nil. + */ +@property (nonatomic, nullable) NSNumber *maximumZoomLevel; + +/** + An `NSString` object that contains an attribution to be displayed when the map is + shown to a user. The default value is nil. + */ +@property (nonatomic, copy, nullable) NSString *attribution; + +/** + An `MGLTileSetScheme` value that contains an enumeration (either + `MGLTileSetSchemeXYZ` or `MGLTileSetSchemeTMS`) that influences the y direction + of the tile coordinates. The default is `MGLTileSetSchemeXYZ`. + */ +@property (nonatomic) MGLTileSetScheme scheme; + +/** + Initializes and returns a new tile set object. + + @param tileURLTemplates An `NSArray` of `NSString` objects that represent the + tile templates. + @return The initialized tile set object. + */ +- (instancetype)initWithTileURLTemplates:(NS_ARRAY_OF(NSString *) *)tileURLTemplates; + +/** + Initializes and returns a new tile set object. + + @param tileURLTemplates An `NSArray` of `NSString` objects that represent the + tile templates. + @param minimumZoomLevel An `NSUInteger`; specifies the minimum zoom level at + which to display tiles. + @param maximumZoomLevel An `NSUInteger`; specifies the maximum zoom level at + which to display tiles. + @return The initialized tile set object. + */ +- (instancetype)initWithTileURLTemplates:(NS_ARRAY_OF(NSString *) *)tileURLTemplates minimumZoomLevel:(NSUInteger)minimumZoomLevel maximumZoomLevel:(NSUInteger)maximumZoomLevel; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLTileSet.mm b/platform/darwin/src/MGLTileSet.mm new file mode 100644 index 0000000000..b2359bb92d --- /dev/null +++ b/platform/darwin/src/MGLTileSet.mm @@ -0,0 +1,87 @@ +#import "MGLTileSet.h" + +#include <mbgl/util/tileset.hpp> + +@implementation MGLTileSet + +- (instancetype)initWithTileURLTemplates:(NS_ARRAY_OF(NSString *) *)tileURLTemplates +{ + if (self = [super init]) + { + _tileURLTemplates = tileURLTemplates; + } + return self; +} + +- (instancetype)initWithTileURLTemplates:(NS_ARRAY_OF(NSString *) *)tileURLTemplates minimumZoomLevel:(NSUInteger)minimumZoomLevel maximumZoomLevel:(NSUInteger)maximumZoomLevel +{ + if (minimumZoomLevel > maximumZoomLevel) + { + [[NSException exceptionWithName:@"Invalid minimumZoomLevel" + reason:@"minimumZoomLevel must be less than maximumZoomLevel" + userInfo:nil] raise]; + return nil; + } + + if (self = [super init]) + { + _tileURLTemplates = tileURLTemplates; + _minimumZoomLevel = @(minimumZoomLevel); + _maximumZoomLevel = @(maximumZoomLevel); + } + return self; +} + +- (void)setMinimumZoomLevel:(NSNumber *)minimumZoomLevel +{ + if (self.maximumZoomLevel && [minimumZoomLevel integerValue] > [self.maximumZoomLevel integerValue]) + { + [[NSException exceptionWithName:@"Invalid minimumZoomLevel" + reason:@"minimumZoomLevel must be less than maximumZoomLevel" + userInfo:nil] raise]; + return; + } + + _minimumZoomLevel = minimumZoomLevel; +} + +- (void)setMaximumZoomLevel:(NSNumber *)maximumZoomLevel +{ + if (self.maximumZoomLevel && [maximumZoomLevel integerValue] < [self.maximumZoomLevel integerValue]) + { + [[NSException exceptionWithName:@"Invalid minimumZoomLevel" + reason:@"minimumZoomLevel must be less than maximumZoomLevel" + userInfo:nil] raise]; + } + + _maximumZoomLevel = maximumZoomLevel; +} + +- (mbgl::Tileset)mbglTileset +{ + mbgl::Tileset tileset; + + for (NSString *tileURLTemplate in self.tileURLTemplates) + { + tileset.tiles.push_back(tileURLTemplate.UTF8String); + } + + // set the minimum / maximum zoom range to the values specified by this class if they + // were set. otherwise, use the core objects default values + uint8_t minimumZoomLevel = self.minimumZoomLevel ? [self.minimumZoomLevel unsignedIntegerValue] : tileset.zoomRange.min; + uint8_t maximumZoomLevel = self.minimumZoomLevel ? [self.maximumZoomLevel unsignedIntegerValue] : tileset.zoomRange.max; + tileset.zoomRange = mbgl::Range<uint8_t>(minimumZoomLevel, maximumZoomLevel); + + if (self.attribution) + { + tileset.attribution = self.attribution.UTF8String; + } + + if (self.scheme == MGLTileSetSchemeTMS) { + tileset.scheme = mbgl::Tileset::Scheme::TMS; + } + + return tileset; +} + +@end diff --git a/platform/darwin/src/MGLTileSet_Private.h b/platform/darwin/src/MGLTileSet_Private.h new file mode 100644 index 0000000000..6a14d428db --- /dev/null +++ b/platform/darwin/src/MGLTileSet_Private.h @@ -0,0 +1,9 @@ +#import "MGLTileSet.h" + +#include <mbgl/util/tileset.hpp> + +@interface MGLTileSet (Private) + +- (mbgl::Tileset)mbglTileset; + +@end
\ No newline at end of file diff --git a/platform/darwin/src/MGLVectorSource.h b/platform/darwin/src/MGLVectorSource.h index 05e041511e..2402208499 100644 --- a/platform/darwin/src/MGLVectorSource.h +++ b/platform/darwin/src/MGLVectorSource.h @@ -1,4 +1,9 @@ #import "MGLSource.h" +#import "MGLTypes.h" + +@class MGLTileSet; + +NS_ASSUME_NONNULL_BEGIN /** A vector tile source. Tiles must be in Mapbox Vector Tile format. @@ -9,6 +14,7 @@ @interface MGLVectorSource : MGLSource @property (nonatomic, readonly, copy) NSURL *URL; +@property (nonatomic, readonly, nullable) MGLTileSet *tileSet; /** Initializes and returns a vector source from a remote url. @@ -20,4 +26,8 @@ */ - (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url; +- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier tileSet:(MGLTileSet *)tileSet; + @end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLVectorSource.mm b/platform/darwin/src/MGLVectorSource.mm index d0c9b0126a..2ab9627ef5 100644 --- a/platform/darwin/src/MGLVectorSource.mm +++ b/platform/darwin/src/MGLVectorSource.mm @@ -1,6 +1,8 @@ #import "MGLVectorSource.h" #import "MGLSource_Private.h" +#import "MGLTileSet_Private.h" +#import "NSURL+MGLAdditions.h" #include <mbgl/style/sources/vector_source.hpp> @@ -8,16 +10,39 @@ static NSString *MGLVectorSourceType = @"vector"; -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url { - if (self = [super initWithSourceIdentifier:sourceIdentifier]) { +- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url +{ + if (self = [super initWithSourceIdentifier:sourceIdentifier]) + { _URL = url; } return self; } +- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier tileSet:(MGLTileSet *)tileSet +{ + if (self = [super initWithSourceIdentifier:sourceIdentifier]) + { + _tileSet = tileSet; + } + return self; +} + - (std::unique_ptr<mbgl::style::Source>)mbglSource { - auto source = std::make_unique<mbgl::style::VectorSource>(self.sourceIdentifier.UTF8String, self.URL.absoluteString.UTF8String); + std::unique_ptr<mbgl::style::VectorSource> source; + + if (self.URL) + { + source = std::make_unique<mbgl::style::VectorSource>(self.sourceIdentifier.UTF8String, + self.URL.mgl_URLByStandardizingScheme.absoluteString.UTF8String); + } + else + { + source = std::make_unique<mbgl::style::VectorSource>(self.sourceIdentifier.UTF8String, + self.tileSet.mbglTileset); + } + return std::move(source); } |