Removed redundant initializers and setters

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”.
This commit is contained in:
Minh Nguyễn
2015-03-31 15:48:01 -07:00
parent 6a1e7d5707
commit e18e4b0de5
3 changed files with 54 additions and 126 deletions

View File

@@ -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;
/** 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;
/** Returns the raw JSON style as a native dictionary object. */
- (NSDictionary *)getRawStyle;
/** Returns the URLs to the styles bundled with the library. */
- (NSArray *)bundledStyleURLs;
/** 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;
/** Sets the map style to a named, bundled style.
* @param styleName The map style name to use. */
@property (nonatomic) IBInspectable NSString *styleName;
/** 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://<user.style>`). */
/** 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://<user.style>`). To display the default style, set this property to `nil`.
*/
@property (nonatomic) NSURL *styleURL;
#pragma mark - Annotating the Map

View File

@@ -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];
}

View File

@@ -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 () <UIGestureRecognizerDelegate, GLKViewDelegate, CLLocationManagerDelegate>
@@ -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<uint32_t> annotationsNearbyLastTap;
@property (nonatomic, weak) id <MGLAnnotation> 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)
{
styleURL = MGLURLForBundledStyleNamed([NSString stringWithFormat:@"%@-v%@",
MGLDefaultStyleName.lowercaseString,
MGLStyleVersion]);
}
if ( ! [styleURL scheme])
{
mbglMap->setStyleURL(std::string("asset://") + styleURLString);
}
else
{
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
- (NSArray *)bundledStyleURLs
{
const std::string styleJSON = mbglMap->getStyleJSON();
if ( ! _bundledStyleURLs)
{
NSString *stylesPath = [[MGLMapView resourceBundlePath] stringByAppendingPathComponent:@"styles"];
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
{
if (!_bundledStyleNames) {
NSString *stylesPath = [[MGLMapView resourceBundlePath] stringByAppendingString:@"/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