summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNadia Barbosa <nadiabarbosa@me.com>2019-05-16 18:01:21 -0700
committerNadia Barbosa <nadiabarbosa@me.com>2019-05-16 18:01:21 -0700
commit3be40ad51a8045366d0f3a91b7f74426400ca1a8 (patch)
treecde2d0135ee949f46b918781d50ef850044d272d
parent46e14f0110b810919becc1b9fd412121ba385517 (diff)
downloadqtlocation-mapboxgl-3be40ad51a8045366d0f3a91b7f74426400ca1a8.tar.gz
Add draft of MGLStyleInitializers
-rw-r--r--platform/darwin/src/MGLStyle.h28
-rw-r--r--platform/darwin/src/MGLStyle.mm50
-rw-r--r--platform/darwin/test/MGLStyleTests.mm3
-rw-r--r--platform/ios/src/MGLMapView.h11
-rw-r--r--platform/ios/src/MGLMapView.mm15
5 files changed, 104 insertions, 3 deletions
diff --git a/platform/darwin/src/MGLStyle.h b/platform/darwin/src/MGLStyle.h
index f3a4db0773..5396a60eb4 100644
--- a/platform/darwin/src/MGLStyle.h
+++ b/platform/darwin/src/MGLStyle.h
@@ -61,6 +61,28 @@ FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLRedundantSourceIdentifier
MGL_EXPORT
@interface MGLStyle : NSObject
+#pragma mark Creating Instances
+
+/**
+ Initializes and returns a new map style with a given URL.
+
+ @param styleURL The URL used to initialize a map style,
+ which must point to a JSON object that conforms to the
+ <a href="https://docs.mapbox.com/mapbox-gl-js/style-spec/">Mapbox Style Specification</a>.
+*/
+- (instancetype)initWithURL:(NSURL *)styleURL;
+
+/**
+ Initializes and returns a new map style with a given data object.
+
+ @param data String data containing JSON that conforms to the
+ <a href="https://docs.mapbox.com/mapbox-gl-js/style-spec/">Mapbox Style Specification</a> .
+ @param encoding The encoding used by `data`.
+ @param outError Upon return, if an error has occurred, a pointer to an
+ `NSError` object describing the error. Pass in `NULL` to ignore any error.
+*/
+- (instancetype)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding error:(NSError * _Nullable *)outError;
+
#pragma mark Accessing Default Styles
/**
@@ -257,6 +279,12 @@ MGL_EXPORT
*/
@property (readonly, copy, nullable) NSString *name;
+/**
+ The <a href="https://docs.mapbox.com/help/glossary/style-url/">style URL</a> associated
+ with the style.
+ */
+@property (readonly, strong, nullable) NSURL *URL;
+
#pragma mark Managing Sources
/**
diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm
index 5768a7aaa7..2208547190 100644
--- a/platform/darwin/src/MGLStyle.mm
+++ b/platform/darwin/src/MGLStyle.mm
@@ -14,6 +14,8 @@
#import "MGLBackgroundStyleLayer.h"
#import "MGLOpenGLStyleLayer.h"
#import "MGLStyleLayerManager.h"
+#import "MGLRendererConfiguration.h"
+#import "MGLOfflineStorage_Private.h"
#import "MGLSource.h"
#import "MGLSource_Private.h"
@@ -38,6 +40,8 @@
#include <mbgl/style/sources/raster_source.hpp>
#include <mbgl/style/sources/raster_dem_source.hpp>
#include <mbgl/style/sources/image_source.hpp>
+#include <mbgl/storage/resource_options.hpp>
+#include <mbgl/storage/file_source.hpp>
#import "NSDate+MGLAdditions.h"
@@ -79,7 +83,7 @@ const MGLExceptionName MGLRedundantSourceIdentifierException = @"MGLRedundantSou
@property (nonatomic, readonly, weak) MGLMapView *mapView;
@property (nonatomic, readonly) mbgl::style::Style *rawStyle;
-@property (readonly, copy, nullable) NSURL *URL;
+@property (readwrite, strong, nullable) NSURL *URL;
@property (nonatomic, readwrite, strong) NSMutableDictionary<NSString *, MGLOpenGLStyleLayer *> *openGLLayers;
@property (nonatomic) NSMutableDictionary<NSString *, NSDictionary<NSObject *, MGLTextLanguage *> *> *localizedLayersByIdentifier;
@@ -87,6 +91,50 @@ const MGLExceptionName MGLRedundantSourceIdentifierException = @"MGLRedundantSou
@implementation MGLStyle
+#pragma mark Creating instances
+
+- (instancetype)initWithURL:(NSURL *)styleURL {
+ if (self = [super init]) {
+ [self commonInit];
+
+ self.URL = styleURL;
+ _URL = styleURL;
+ }
+
+ return self;
+}
+
+- (instancetype)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding error:(NSError * _Nullable *)outError {
+ if (self = [super init]) {
+ [self commonInit];
+ NSString *string = [[NSString alloc] initWithData:data encoding:encoding];
+ if (!string) {
+ if (outError) {
+ *outError = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeLoadStyleFailed userInfo:nil];
+ }
+ return nil;
+ }
+
+ _rawStyle->loadJSON(string.UTF8String);
+ }
+
+ return self;
+}
+
+- (void)commonInit {
+ MGLRendererConfiguration *config = [MGLRendererConfiguration currentConfiguration];
+
+ mbgl::ResourceOptions resourceOptions;
+
+ resourceOptions.withCachePath([[MGLOfflineStorage sharedOfflineStorage] mbglCachePath])
+ .withAssetPath([NSBundle mainBundle].resourceURL.path.UTF8String);
+
+ auto fileSource = mbgl::FileSource::getSharedFileSource(resourceOptions);
+ auto style = std::make_unique<mbgl::style::Style>(*fileSource, config.scaleFactor);
+
+ _rawStyle = &*style;
+}
+
#pragma mark Default style URLs
/// @param name The style’s marketing name, written in lower camelCase.
diff --git a/platform/darwin/test/MGLStyleTests.mm b/platform/darwin/test/MGLStyleTests.mm
index 7aaf70a80a..45a0b485e0 100644
--- a/platform/darwin/test/MGLStyleTests.mm
+++ b/platform/darwin/test/MGLStyleTests.mm
@@ -29,7 +29,8 @@
[MGLAccountManager setAccessToken:@"pk.feedcafedeadbeefbadebede"];
NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"one-liner" withExtension:@"json"];
- self.mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) styleURL:styleURL];
+ MGLStyle *mapStyle = [[MGLStyle alloc] initWithURL:styleURL];
+ self.mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) style:mapStyle];
self.mapView.delegate = self;
if (!self.mapView.style) {
_styleLoadingExpectation = [self expectationWithDescription:@"Map view should finish loading style."];
diff --git a/platform/ios/src/MGLMapView.h b/platform/ios/src/MGLMapView.h
index 63bd28fc0c..0e1fe538b8 100644
--- a/platform/ios/src/MGLMapView.h
+++ b/platform/ios/src/MGLMapView.h
@@ -217,6 +217,17 @@ MGL_EXPORT
*/
- (instancetype)initWithFrame:(CGRect)frame styleURL:(nullable NSURL *)styleURL;
+
+/**
+ Initializes and returns a newly allocated map view with the specified frame
+ and style.
+
+ @param frame The frame for the view, measured in points.
+ @param style The style to display. Specify `nil` for the default style.
+ @return An initialized map view.
+*/
+- (instancetype)initWithFrame:(CGRect)frame style:(nullable MGLStyle *)style;
+
#pragma mark Accessing the Delegate
/**
diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm
index fa49a2f280..0c05ee7b08 100644
--- a/platform/ios/src/MGLMapView.mm
+++ b/platform/ios/src/MGLMapView.mm
@@ -355,7 +355,20 @@ public:
MGLLogInfo(@"Starting %@ initialization.", NSStringFromClass([self class]));
MGLLogDebug(@"Initializing frame: %@ styleURL: %@", NSStringFromCGRect(frame), styleURL);
[self commonInit];
- self.styleURL = styleURL;
+ MGLStyle *style = [[MGLStyle alloc] initWithURL:styleURL];
+ self.styleURL = style.URL;
+ MGLLogInfo(@"Finalizing %@ initialization.", NSStringFromClass([self class]));
+ }
+ return self;
+}
+
+- (instancetype)initWithFrame:(CGRect)frame style:(nullable MGLStyle *)style {
+ if (self = [super initWithFrame:frame])
+ {
+ MGLLogInfo(@"Starting %@ initialization.", NSStringFromClass([self class]));
+ MGLLogDebug(@"Initializing frame: %@ style: %@", NSStringFromCGRect(frame), style);
+ [self commonInit];
+ self.style = style;
MGLLogInfo(@"Finalizing %@ initialization.", NSStringFromClass([self class]));
}
return self;