[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
This commit is contained in:
Minh Nguyễn
2015-10-29 12:23:50 -07:00
parent 3b6dc37e39
commit 557b8afb7c
16 changed files with 149 additions and 95 deletions

View File

@@ -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))

View File

@@ -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',

View File

@@ -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.
*

View File

@@ -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

View File

@@ -10,5 +10,6 @@
#import "MGLPolygon.h"
#import "MGLPolyline.h"
#import "MGLShape.h"
#import "MGLStyle.h"
#import "MGLTypes.h"
#import "MGLUserLocation.h"

View File

@@ -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

View File

@@ -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;
@@ -79,7 +66,7 @@ static NSUInteger const kStyleVersion = 8;
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;
@@ -323,14 +310,11 @@ static NSUInteger const kStyleVersion = 8;
{
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

View File

@@ -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>
@@ -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);

View File

@@ -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' : {

View File

@@ -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>
@@ -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>
@@ -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);

View File

@@ -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' : {

View File

@@ -1,16 +0,0 @@
#include "default_styles.hpp"
namespace mbgl {
namespace util {
const std::vector<std::pair<std::string, std::string>> defaultStyles = {
{ "mapbox://styles/mapbox/streets-v8", "Streets" },
{ "mapbox://styles/mapbox/emerald-v8", "Emerald" },
{ "mapbox://styles/mapbox/light-v8", "Light" },
{ "mapbox://styles/mapbox/dark-v8", "Dark" },
{ "mapbox://styles/mapbox/satellite-v8", "Satellite" },
{ "mapbox://styles/mapbox/satellite-hybrid-v8", "Hybrid" },
};
} // end namespace util
} // end namespace mbgl

View File

@@ -1,17 +0,0 @@
#ifndef MBGL_PLATFORM_DEFAULT_STYLES
#define MBGL_PLATFORM_DEFAULT_STYLES
#include <vector>
#include <string>
namespace mbgl {
namespace util {
// A list of default styles, with the first string being the URL
// and the second being the user-visible name.
extern const std::vector<std::pair<std::string, std::string>> defaultStyles;
} // end namespace util
} // end namespace mbgl
#endif

View File

@@ -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"
@@ -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;
@@ -60,11 +58,6 @@ NSString *const MGLAnnotationIDKey = @"MGLAnnotationIDKey";
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)
@@ -190,9 +183,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
if ( ! styleURL)
{
styleURL = MGLURLForBundledStyleNamed([NSString stringWithFormat:@"%@-v%lu",
MGLDefaultStyleName.lowercaseString,
(unsigned long)MGLStyleVersion]);
styleURL = [MGLStyle streetsStyleURL];
}
if ( ! [styleURL scheme])
@@ -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];
}
}

30
platform/ios/MGLStyle.mm Normal file
View File

@@ -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

View File

@@ -0,0 +1,16 @@
#include <mbgl/util/default_styles.hpp>
namespace mbgl {
namespace util {
namespace default_styles {
const DefaultStyle streets = { "mapbox://styles/mapbox/streets-v8", "Streets" };
const DefaultStyle emerald = { "mapbox://styles/mapbox/emerald-v8", "Emerald" };
const DefaultStyle light = { "mapbox://styles/mapbox/light-v8", "Light" };
const DefaultStyle dark = { "mapbox://styles/mapbox/dark-v8", "Dark" };
const DefaultStyle satellite = { "mapbox://styles/mapbox/satellite-v8", "Satellite" };
const DefaultStyle hybrid = { "mapbox://styles/mapbox/satellite-hybrid-v8", "Hybrid" };
}
} // end namespace util
} // end namespace mbgl