From e18e4b0de5f601cdfb07eed209e64f88a114e492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Tue, 31 Mar 2015 15:48:01 -0700 Subject: Removed redundant initializers and setters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change removes most of the ways you used to be able to apply a style to the map. Building on #1163, `styleURL` (HTTP(S), mapbox:, asset:) is the canonical way to apply a style, and `mapID` is a convenient shorthand for Mapbox-hosted styles. A relative style URL is interpreted as a path relative to the app’s main bundle. We now construct asset: URLs in lieu of “bundled style names”. --- include/mbgl/ios/MGLMapView.h | 43 ++++---------- ios/app/MBXViewController.mm | 3 +- platform/ios/MGLMapView.mm | 132 +++++++++++++----------------------------- 3 files changed, 53 insertions(+), 125 deletions(-) diff --git a/include/mbgl/ios/MGLMapView.h b/include/mbgl/ios/MGLMapView.h index 354e32cc5a..dcf1313a8c 100644 --- a/include/mbgl/ios/MGLMapView.h +++ b/include/mbgl/ios/MGLMapView.h @@ -19,20 +19,6 @@ /** @name Initializing a Map View */ -/** Initialize a map view with a given frame, style, and access token. -* @param frame The frame with which to initialize the map view. -* @param accessToken A Mapbox API access token. -* @param styleJSON The map stylesheet as JSON text. -* @return An initialized map view, or `nil` if the map view was unable to be initialized. */ -- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken styleJSON:(NSString *)styleJSON; - -/** Initialize a map view with a given frame, bundled style name, and access token. -* @param frame The frame with which to initialize the map view. -* @param accessToken A Mapbox API access token. -* @param styleName The map style name to use. -* @return An initialized map view, or `nil` if the map view was unable to be initialized. */ -- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken bundledStyleNamed:(NSString *)styleName; - /** Initialize a map view with a given frame, style URL, and access token. * @param frame The frame with which to initialize the map view. * @param accessToken A Mapbox API access token. @@ -178,26 +164,19 @@ /** @name Styling the Map */ -/** Sets the map style. -* @param styleJSON The map stylesheet as JSON text. */ -- (void)setStyleJSON:(NSString *)styleJSON; - -/** Returns the raw JSON style as a native dictionary object. */ -- (NSDictionary *)getRawStyle; - -/** Sets the raw JSON style as a native dictionary object with a transition animation. -* @param style The style JSON as a dictionary object. */ -- (void)setRawStyle:(NSDictionary *)style; - -/** Returns the names of the styles bundled with the library. */ -- (NSArray *)bundledStyleNames; +/** Mapbox map ID of the style currently displayed in the receiver, or `nil` if the style does not have a map ID. + + The style may lack a map ID if it is located at an HTTP, HTTPS, or asset: URL. Use -styleURL to get the URL in these cases. + */ +@property (nonatomic) IBInspectable NSString *mapID; -/** Sets the map style to a named, bundled style. -* @param styleName The map style name to use. */ -@property (nonatomic) IBInspectable NSString *styleName; +/** Returns the URLs to the styles bundled with the library. */ +- (NSArray *)bundledStyleURLs; -/** Sets the map style URL to use. -* @param styleURL The map style URL to use. Can be either an HTTP/HTTPS URL or a Mapbox map ID style URL (`mapbox://`). */ +/** URL of the style currently displayed in the receiver. + + The URL may be a full HTTP or HTTPS URL or a Mapbox URL indicating the style’s map ID (`mapbox://`). To display the default style, set this property to `nil`. + */ @property (nonatomic) NSURL *styleURL; #pragma mark - Annotating the Map diff --git a/ios/app/MBXViewController.mm b/ios/app/MBXViewController.mm index 1461439697..28ac68589a 100644 --- a/ios/app/MBXViewController.mm +++ b/ios/app/MBXViewController.mm @@ -16,7 +16,6 @@ static NSArray *const kStyleNames = @[ @"Basic", @"Outdoors", @"Satellite", - @"Hybrid", ]; static NSString *const kStyleVersion = @"7"; @@ -246,7 +245,7 @@ mbgl::Settings_NSUserDefaults *settings = nullptr; styleName = [kStyleNames objectAtIndex:index]; } - self.mapView.styleName = [NSString stringWithFormat:@"%@-v%@", styleName.lowercaseString, kStyleVersion]; + self.mapView.styleURL = [NSURL URLWithString:[NSString stringWithFormat:@"asset://styles/%@-v%@.json", styleName.lowercaseString, kStyleVersion]]; [titleButton setTitle:styleName forState:UIControlStateNormal]; } diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index b6519178d6..67a62680e8 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -53,6 +53,11 @@ const CGFloat MGLMinimumZoom = 3; NSString *const MGLAnnotationIDKey = @"MGLAnnotationIDKey"; +static NSURL *MGLURLForBundledStyleNamed(NSString *styleName) +{ + return [NSURL URLWithString:[NSString stringWithFormat:@"asset://styles/%@.json", styleName]]; +} + #pragma mark - Private - @interface MGLMapView () @@ -67,7 +72,7 @@ NSString *const MGLAnnotationIDKey = @"MGLAnnotationIDKey"; @property (nonatomic) UIPinchGestureRecognizer *pinch; @property (nonatomic) UIRotationGestureRecognizer *rotate; @property (nonatomic) UILongPressGestureRecognizer *quickZoom; -@property (nonatomic) NSMutableArray *bundledStyleNames; +@property (nonatomic) NSMutableArray *bundledStyleURLs; @property (nonatomic) NSMapTable *annotationIDsByAnnotation; @property (nonatomic) std::vector annotationsNearbyLastTap; @property (nonatomic, weak) id selectedAnnotation; @@ -101,7 +106,7 @@ MBGLView *mbglView = nullptr; mbgl::SQLiteCache *mbglFileCache = nullptr; mbgl::DefaultFileSource *mbglFileSource = nullptr; -- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken styleJSON:(NSString *)styleJSON +- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken { self = [super initWithFrame:frame]; @@ -109,55 +114,38 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr; { [self setAccessToken:accessToken]; - if (styleJSON || accessToken) + if (accessToken) { // If style is set directly, pass it on. If not, if we have an access // token, we can pass nil and use the default style. // - [self setStyleJSON:styleJSON]; + self.styleURL = nil; } } return self; } -- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken bundledStyleNamed:(NSString *)styleName -{ - self = [super initWithFrame:frame]; - - if (self && [self commonInit]) - { - [self setAccessToken:accessToken]; - if (styleName) [self setStyleName:styleName]; - } - - return self; -} - - (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken styleURL:(NSURL *)styleURL { self = [super initWithFrame:frame]; if (self && [self commonInit]) { - if (accessToken) [self setAccessToken:accessToken]; - if (styleURL) [self setStyleURL:styleURL]; + self.accessToken = accessToken; + self.styleURL = styleURL; } return self; } -- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken -{ - return [self initWithFrame:frame accessToken:accessToken styleJSON:nil]; -} - - (instancetype)initWithCoder:(NSCoder *)decoder { self = [super initWithCoder:decoder]; if (self && [self commonInit]) { + self.styleURL = nil; return self; } @@ -175,16 +163,8 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr; [MGLMapboxEvents setToken:accessToken.mgl_stringOrNilIfEmpty]; } -- (void)setStyleJSON:(NSString *)styleJSON -{ - if ( ! styleJSON) - { - [self setStyleName:[NSString stringWithFormat:@"%@-v%@", MGLDefaultStyleName.lowercaseString, MGLStyleVersion]]; - } - else - { - mbglMap->setStyleJSON((std::string)[styleJSON UTF8String]); - } ++ (NSSet *)keyPathsForValuesAffectingStyleURL { + return [NSSet setWithObjects:@"mapID", @"accessToken", nil]; } - (NSURL *)styleURL @@ -195,16 +175,20 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr; - (void)setStyleURL:(NSURL *)styleURL { - std::string styleURLString([[styleURL absoluteString] UTF8String]); - - if ( ! [styleURL scheme]) + if ( ! styleURL) { - mbglMap->setStyleURL(std::string("asset://") + styleURLString); + styleURL = MGLURLForBundledStyleNamed([NSString stringWithFormat:@"%@-v%@", + MGLDefaultStyleName.lowercaseString, + MGLStyleVersion]); } - else + + if ( ! [styleURL scheme]) { - mbglMap->setStyleURL(styleURLString); + // Assume a relative path into the developer’s bundle. + styleURL = [[NSBundle mainBundle] URLForResource:styleURL.path withExtension:nil]; } + + mbglMap->setStyleURL([[styleURL absoluteString] UTF8String]); } - (BOOL)commonInit @@ -1291,69 +1275,35 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng) #pragma mark - Styling - -- (NSDictionary *)getRawStyle -{ - const std::string styleJSON = mbglMap->getStyleJSON(); - - return [NSJSONSerialization JSONObjectWithData:[@(styleJSON.c_str()) dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; -} - -- (void)setRawStyle:(NSDictionary *)style -{ - NSData *data = [NSJSONSerialization dataWithJSONObject:style options:0 error:nil]; - - [self setStyleJSON:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]]; -} - -- (NSArray *)bundledStyleNames +- (NSArray *)bundledStyleURLs { - if (!_bundledStyleNames) { - NSString *stylesPath = [[MGLMapView resourceBundlePath] stringByAppendingString:@"/styles"]; + if ( ! _bundledStyleURLs) + { + NSString *stylesPath = [[MGLMapView resourceBundlePath] stringByAppendingPathComponent:@"styles"]; - _bundledStyleNames = [NSMutableArray array]; + _bundledStyleURLs = [NSMutableArray array]; NSArray *bundledStyleNamesWithExtensions = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:stylesPath error:nil]; - NSString *hybridStylePrefix = @"hybrid-"; - NSString *satelliteStylePrefix = @"satellite-"; - for (NSString *fileName in bundledStyleNamesWithExtensions) { - NSString *styleName = [fileName stringByDeletingPathExtension]; - [_bundledStyleNames addObject:styleName]; - - // Add satellite raster & "hybrid" (satellite raster + vector contours & labels) - if ([styleName hasPrefix:satelliteStylePrefix]) { - [_bundledStyleNames addObject:[hybridStylePrefix stringByAppendingString:[styleName substringFromIndex:[satelliteStylePrefix length]]]]; - } + for (NSString *fileName in bundledStyleNamesWithExtensions) + { + [_bundledStyleURLs addObject:MGLURLForBundledStyleNamed([fileName stringByDeletingPathExtension])]; } } - return [NSArray arrayWithArray:_bundledStyleNames]; + return [NSArray arrayWithArray:_bundledStyleURLs]; } -- (NSString *)styleName -{ ++ (NSSet *)keyPathsForValuesAffectingMapID { + return [NSSet setWithObjects:@"styleURL", @"accessToken", nil]; +} + +- (NSString *)mapID { NSURL *styleURL = self.styleURL; - NSString *styleName; - if ([styleURL.scheme isEqualToString:@"asset"]) - { - styleName = styleURL.lastPathComponent.stringByDeletingPathExtension; - } - else if ([styleURL.scheme isEqualToString:@"mapbox"]) { - styleName = styleURL.host; - } - return styleName.mgl_stringOrNilIfEmpty; + return [styleURL.scheme isEqualToString:@"mapbox"] ? styleURL.host.mgl_stringOrNilIfEmpty : nil; } -- (void)setStyleName:(NSString *)styleName -{ - NSString *hybridStylePrefix = @"hybrid-"; - BOOL isHybrid = [styleName hasPrefix:hybridStylePrefix]; - if (isHybrid) { - styleName = [@"satellite-" stringByAppendingString:[styleName substringFromIndex:[hybridStylePrefix length]]]; - } - [self setStyleURL:[NSURL URLWithString:[NSString stringWithFormat:@"styles/%@.json", styleName]]]; - if (isHybrid) { - [self setStyleClasses:@[@"contours", @"labels"]]; - } +- (void)setMapID:(NSString *)mapID { + self.styleURL = [NSURL URLWithString:[NSString stringWithFormat:@"mapbox://%@", mapID]]; } - (NSArray *)getAppliedStyleClasses -- cgit v1.2.1