Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Replace bundled styles with mapbox: style URLs #2746

Merged
merged 3 commits into from
Oct 30, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[core][iOS] Source iOS styles from default_styles
Moved mbgl::util::default_styles to a more appropriate location, where iOS platform code can also find it. Moved -[MGLMapView bundledStyleURLs] (which is now deprecated) and the style switcher in iosapp to default_styles.

Added a collection of convenience methods for getting style URLs. It makes little sense to layer an enum atop this, as MapKit does, because MGLMapView styles aren’t limited to this set. A good analogy is UIColor. This also makes for a good entry point for future runtime styling APIs.

Introduced independent constants for each default style, because it’s more common to need access to a particular style than to iterate over them. This fact is apparent in the MGLStyle class, which now uses macros and assertions to ensure that it’s kept up-to-date with changes in default_styles.

/ref #1462
  • Loading branch information
1ec5 committed Oct 30, 2015
commit 557b8afb7c310a6330f741ca0f38fcec098f3156
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## iOS master

- The `styleID` property has been removed from MGLMapView. Instead, set the `styleURL` property to an NSURL in the form `mapbox://styles/STYLE_ID`. If you previously set the style ID in Interface Builder’s Attributes inspector, delete the `styleID` entry from the User Defined Runtime Attributes section of the Identity inspector, then set the new “Style URL” inspectable to a value in the form `mapbox://styles/STYLE_ID`. ([#2632](https://github.com/mapbox/mapbox-gl-native/pull/2632))
- Default styles such as Streets are no longer bundled with the SDK; instead, they are loaded at runtime from the style API on mapbox.com. As always, you can use these default styles with any valid access token, and Streets continues to be `MGLMapView`’s initial style. The `bundledStyleURLs` property on `MGLMapView` has been deprecated in favor of several class methods on `MGLStyle` that provide direct access to the default styles. ([#2746](https://github.com/mapbox/mapbox-gl-native/pull/2746))
- The SDK now builds with Bitcode enabled. ([#2332](https://github.com/mapbox/mapbox-gl-native/issues/2332))
- The double-tap-drag gesture for zooming in and out is now consistent with the Google Maps SDK. ([#2153](https://github.com/mapbox/mapbox-gl-native/pull/2153))
- A new `MGLAnnotationImage.enabled` property allows you to disable touch events on individual annotations. ([#2501](https://github.com/mapbox/mapbox-gl-native/pull/2501))
Expand Down
2 changes: 2 additions & 0 deletions gyp/platform-ios.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
'../platform/ios/MGLShape.m',
'../include/mbgl/ios/MGLAnnotationImage.h',
'../platform/ios/MGLAnnotationImage.m',
'../include/mbgl/ios/MGLStyle.h',
'../platform/ios/MGLStyle.mm',
'../platform/ios/NSBundle+MGLAdditions.h',
'../platform/ios/NSBundle+MGLAdditions.m',
'../platform/ios/NSException+MGLAdditions.h',
Expand Down
5 changes: 3 additions & 2 deletions include/mbgl/ios/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ IB_DESIGNABLE

@property (nonatomic, nullable) NSString *styleID __attribute__((unavailable("Set styleURL to an NSURL of the form <mapbox://styles/STYLE_ID>, where STYLE_ID would have been the value of this property.")));

/** URLs of the styles bundled with the library. */
@property (nonatomic, readonly) NS_ARRAY_OF(NSURL *) *bundledStyleURLs;
/** URLs of the styles bundled with the library.
@deprecated Call the relevant class method of `MGLStyle` for the URL of a particular default style. */
@property (nonatomic, readonly) NS_ARRAY_OF(NSURL *) *bundledStyleURLs __attribute__((deprecated("Call the relevant class method of MGLStyle for the URL of a particular default style.")));

/** URL of the style currently displayed in the receiver.
*
Expand Down
36 changes: 36 additions & 0 deletions include/mbgl/ios/MGLStyle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#import "Mapbox.h"

NS_ASSUME_NONNULL_BEGIN

/** A collection of convenience methods for creating style URLs of default styles provided by Mapbox. These instances of NSURL are cached. */
@interface MGLStyle : NSObject

/** Returns the Streets style URL.
* Mapbox Streets is a complete base map, perfect for incorporating your own data. */
+ (NSURL *)streetsStyleURL;

/** Returns the Emerald style URL.
* Emerald is a versatile style with emphasis on road networks and public transportation. */
+ (NSURL *)emeraldStyleURL;

/** Returns the Light style URL.
* Light is a subtle, light-colored backdrop for data visualizations. */
+ (NSURL *)lightStyleURL;

/** Returns the Dark style URL.
* Dark is a subtle, dark-colored backdrop for data visualizations. */
+ (NSURL *)darkStyleURL;

/** Returns the Satellite style URL.
* Mapbox Satellite is a beautiful global satellite and aerial imagery layer. */
+ (NSURL *)satelliteStyleURL;

/** Returns the Hybrid style URL.
* Hybrid combines the global satellite and aerial imagery of Mapbox Satellite with unobtrusive labels. */
+ (NSURL *)hybridStyleURL;

- (instancetype)init NS_UNAVAILABLE;

@end

NS_ASSUME_NONNULL_END
1 change: 1 addition & 0 deletions include/mbgl/ios/Mapbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
#import "MGLPolygon.h"
#import "MGLPolyline.h"
#import "MGLShape.h"
#import "MGLStyle.h"
#import "MGLTypes.h"
#import "MGLUserLocation.h"
32 changes: 32 additions & 0 deletions include/mbgl/util/default_styles.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef MBGL_PLATFORM_DEFAULT_STYLES
#define MBGL_PLATFORM_DEFAULT_STYLES

#include <vector>
#include <string>

namespace mbgl {
namespace util {
namespace default_styles {

struct DefaultStyle {
const char* url;
const char* name;
};

extern const DefaultStyle streets;
extern const DefaultStyle emerald;
extern const DefaultStyle light;
extern const DefaultStyle dark;
extern const DefaultStyle satellite;
extern const DefaultStyle hybrid;

const DefaultStyle orderedStyles[] = {
streets, emerald, light, dark, satellite, hybrid,
};
const size_t numOrderedStyles = sizeof(orderedStyles) / sizeof(DefaultStyle);

} // end namespace default_styles
} // end namespace util
} // end namespace mbgl

#endif
26 changes: 5 additions & 21 deletions ios/app/MBXViewController.mm
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
#import "MBXViewController.h"

#import <mbgl/ios/Mapbox.h>
#import <mbgl/util/default_styles.hpp>

#import <CoreLocation/CoreLocation.h>

static UIColor *const kTintColor = [UIColor colorWithRed:0.120 green:0.550 blue:0.670 alpha:1.000];

static struct MBXStyle {
NSString *name;
NSString *displayName;
} MBXAvailableStyles[] = {
{@"streets", @"Streets"},
{@"emerald", @"Emerald"},
{@"light", @"Light"},
{@"dark", @"Dark"},
{@"satellite", @"Satellite"},
{@"satellite-hybrid", @"Hybrid"},
};

static NSUInteger const kStyleVersion = 8;

@interface MBXViewController () <UIActionSheetDelegate, MGLMapViewDelegate>

@property (nonatomic) MGLMapView *mapView;
Expand Down Expand Up @@ -79,7 +66,7 @@ - (void)viewDidLoad

UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[titleButton setFrame:CGRectMake(0, 0, 150, 40)];
[titleButton setTitle:MBXAvailableStyles[self.styleIndex].displayName forState:UIControlStateNormal];
[titleButton setTitle:@(mbgl::util::default_styles::orderedStyles[self.styleIndex].name) forState:UIControlStateNormal];
[titleButton setTitleColor:kTintColor forState:UIControlStateNormal];
[titleButton addTarget:self action:@selector(cycleStyles) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleButton;
Expand Down Expand Up @@ -323,14 +310,11 @@ - (void)cycleStyles
{
UIButton *titleButton = (UIButton *)self.navigationItem.titleView;

self.styleIndex = (self.styleIndex + 1) % (sizeof(MBXAvailableStyles) / sizeof(MBXAvailableStyles[0]));
self.styleIndex = (self.styleIndex + 1) % mbgl::util::default_styles::numOrderedStyles;

self.mapView.styleURL = [NSURL URLWithString:
[NSString stringWithFormat:@"mapbox://styles/mapbox/%@-v%lu",
MBXAvailableStyles[self.styleIndex].name,
(unsigned long)kStyleVersion]];
self.mapView.styleURL = [NSURL URLWithString:@(mbgl::util::default_styles::orderedStyles[self.styleIndex].url)];

[titleButton setTitle:MBXAvailableStyles[self.styleIndex].displayName forState:UIControlStateNormal];
[titleButton setTitle:@(mbgl::util::default_styles::orderedStyles[self.styleIndex].name) forState:UIControlStateNormal];
}

- (void)locateUser
Expand Down
18 changes: 9 additions & 9 deletions linux/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <mbgl/mbgl.hpp>
#include "../platform/default/default_styles.hpp"
#include <mbgl/util/default_styles.hpp>
#include <mbgl/util/uv.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/platform/platform.hpp>
Expand Down Expand Up @@ -131,22 +131,22 @@ int main(int argc, char *argv[]) {
view->setChangeStyleCallback([&map] () {
static uint8_t currentStyleIndex;

if (++currentStyleIndex == mbgl::util::defaultStyles.size()) {
if (++currentStyleIndex == mbgl::util::default_styles::numOrderedStyles) {
currentStyleIndex = 0;
}

const auto& newStyle = mbgl::util::defaultStyles[currentStyleIndex];
map.setStyleURL(newStyle.first);
view->setWindowTitle(newStyle.second);
mbgl::util::default_styles::DefaultStyle newStyle = mbgl::util::default_styles::orderedStyles[currentStyleIndex];
map.setStyleURL(newStyle.url);
view->setWindowTitle(newStyle.name);

mbgl::Log::Info(mbgl::Event::Setup, std::string("Changed style to: ") + newStyle.first);
mbgl::Log::Info(mbgl::Event::Setup, std::string("Changed style to: ") + newStyle.name);
});

// Load style
if (style.empty()) {
const auto& newStyle = mbgl::util::defaultStyles.front();
style = newStyle.first;
view->setWindowTitle(newStyle.second);
mbgl::util::default_styles::DefaultStyle newStyle = mbgl::util::default_styles::orderedStyles[0];
style = newStyle.url;
view->setWindowTitle(newStyle.name);
}

map.setStyleURL(style);
Expand Down
2 changes: 0 additions & 2 deletions linux/mapboxgl-app.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
'../platform/default/glfw_view.hpp',
'../platform/default/glfw_view.cpp',
'../platform/default/log_stderr.cpp',
'../platform/default/default_styles.hpp',
'../platform/default/default_styles.cpp',
],

'variables' : {
Expand Down
18 changes: 9 additions & 9 deletions macosx/main.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <mbgl/platform/log.hpp>
#include "../platform/default/default_styles.hpp"
#include <mbgl/platform/platform.hpp>
#include <mbgl/platform/darwin/settings_nsuserdefaults.hpp>
#include <mbgl/platform/darwin/reachability.h>
Expand All @@ -9,6 +8,7 @@
#include <mbgl/storage/network_status.hpp>

#include <mbgl/util/geo.hpp>
#include <mbgl/util/default_styles.hpp>

#import <Foundation/Foundation.h>

Expand Down Expand Up @@ -172,23 +172,23 @@ int main(int argc, char* argv[]) {
view.setChangeStyleCallback([&map, &view] () {
static uint8_t currentStyleIndex;

if (++currentStyleIndex == mbgl::util::defaultStyles.size()) {
if (++currentStyleIndex == mbgl::util::default_styles::numOrderedStyles) {
currentStyleIndex = 0;
}

const auto& newStyle = mbgl::util::defaultStyles[currentStyleIndex];
map.setStyleURL(newStyle.first);
view.setWindowTitle(newStyle.second);
mbgl::util::default_styles::DefaultStyle newStyle = mbgl::util::default_styles::orderedStyles[currentStyleIndex];
map.setStyleURL(newStyle.url);
view.setWindowTitle(newStyle.name);

mbgl::Log::Info(mbgl::Event::Setup, std::string("Changed style to: ") + newStyle.first);
mbgl::Log::Info(mbgl::Event::Setup, std::string("Changed style to: ") + newStyle.name);
});


// Load style
if (style.empty()) {
const auto& newStyle = mbgl::util::defaultStyles.front();
style = newStyle.first;
view.setWindowTitle(newStyle.second);
mbgl::util::default_styles::DefaultStyle newStyle = mbgl::util::default_styles::orderedStyles[0];
style = newStyle.url;
view.setWindowTitle(newStyle.name);
}

map.setStyleURL(style);
Expand Down
2 changes: 0 additions & 2 deletions macosx/mapboxgl-app.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
'../platform/darwin/reachability.m',
'../platform/default/glfw_view.hpp',
'../platform/default/glfw_view.cpp',
'../platform/default/default_styles.hpp',
'../platform/default/default_styles.cpp',
],

'variables' : {
Expand Down
16 changes: 0 additions & 16 deletions platform/default/default_styles.cpp

This file was deleted.

17 changes: 0 additions & 17 deletions platform/default/default_styles.hpp

This file was deleted.

22 changes: 5 additions & 17 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <mbgl/util/constants.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/default_styles.hpp>

#import "Mapbox.h"

Expand All @@ -42,12 +43,9 @@

class MBGLView;

NSString *const MGLDefaultStyleName = @"streets";
NSString *const MGLDefaultStyleMarkerSymbolName = @"default_marker";
NSString *const MGLMapboxSetupDocumentationURLDisplayString = @"mapbox.com/guides/first-steps-ios-sdk";

NSUInteger const MGLStyleVersion = 8;

const NSTimeInterval MGLAnimationDuration = 0.3;
const CGSize MGLAnnotationUpdateViewportOutset = {150, 150};
const CGFloat MGLMinimumZoom = 3;
Expand All @@ -60,11 +58,6 @@
NSString *const MGLAnnotationSymbolKey = @"MGLAnnotationSymbolKey";
NSString *const MGLAnnotationSpritePrefix = @"com.mapbox.sprites.";

static NSURL *MGLURLForBundledStyleNamed(NSString *styleName)
{
return [NSURL URLWithString:[NSString stringWithFormat:@"mapbox://styles/mapbox/%@", styleName]];
}

mbgl::util::UnitBezier MGLUnitBezierForMediaTimingFunction(CAMediaTimingFunction *function)
{
if ( ! function)
Expand Down Expand Up @@ -190,9 +183,7 @@ - (void)setStyleURL:(nullable NSURL *)styleURL

if ( ! styleURL)
{
styleURL = MGLURLForBundledStyleNamed([NSString stringWithFormat:@"%@-v%lu",
MGLDefaultStyleName.lowercaseString,
(unsigned long)MGLStyleVersion]);
styleURL = [MGLStyle streetsStyleURL];
}

if ( ! [styleURL scheme])
Expand Down Expand Up @@ -1980,14 +1971,11 @@ CLLocationCoordinate2D MGLLocationCoordinate2DFromLatLng(mbgl::LatLng latLng)
{
if ( ! _bundledStyleURLs)
{
NSString *stylesPath = [[NSBundle mgl_resourceBundlePath] stringByAppendingPathComponent:@"styles"];

_bundledStyleURLs = [NSMutableArray array];

NSArray *bundledStyleNamesWithExtensions = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:stylesPath error:nil];
for (NSString *fileName in bundledStyleNamesWithExtensions)
for (NSUInteger i = 0; i < mbgl::util::default_styles::numOrderedStyles; i++)
{
[_bundledStyleURLs addObject:MGLURLForBundledStyleNamed([fileName stringByDeletingPathExtension])];
NSURL *styleURL = [NSURL URLWithString:@(mbgl::util::default_styles::orderedStyles[i].url)];
[_bundledStyleURLs addObject:styleURL];
}
}

Expand Down
30 changes: 30 additions & 0 deletions platform/ios/MGLStyle.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#import "MGLStyle.h"

#import <mbgl/util/default_styles.hpp>

@implementation MGLStyle

// name is lowercase
#define MGL_DEFINE_STYLE(name) \
static NSURL *MGLStyleURL_##name; \
+ (NSURL *)name##StyleURL { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
MGLStyleURL_##name = [NSURL URLWithString:@(mbgl::util::default_styles::name.url)]; \
}); \
return MGLStyleURL_##name; \
}

MGL_DEFINE_STYLE(streets)
MGL_DEFINE_STYLE(emerald)
MGL_DEFINE_STYLE(light)
MGL_DEFINE_STYLE(dark)
MGL_DEFINE_STYLE(satellite)
MGL_DEFINE_STYLE(hybrid)

// Make sure all the styles listed in mbgl::util::default_styles::orderedStyles
// are defined above and also declared in MGLStyle.h.
static_assert(6 == mbgl::util::default_styles::numOrderedStyles,
"mbgl::util::default_styles::orderedStyles and MGLStyle have different numbers of styles.");

@end
Loading