Files
MapLibreParticleLab/platform/darwin/src/MGLStyle.mm
Tobrun 2d039d3c6b Merge release-ios-v3.6.0-android-v5.1.0 to master (#9170)
* [android] Update release script to support CircleCI builds (#8950)

* update release script to trigger builds on circleci now

* update release script to trigger builds on circleci now

* [core] When a layer is added, reload its source's tiles

* [android] - keep observer when timeout occurs, make observer param nullable, fixup log messages (#8919)

* [android] - avoid crashing when deleting already deleted region (#8920)

* [android] - update LOST to 2.3.0 (#8872)

* [android] - update proguard config, allow debug mimification, update OkHttp to latest version (#8944)

* [core, android, ios, macos, qt] v10 default styles

Upgraded from v9 default styles to v10 wherever the developer expects to get the latest and greatest, as well as in a couple tests where it may be beneficial to ensure that we can handle a two-digit version number in the style URL.

Cherry-picked from ed54849e99.

* [ios, macos] Updated documented default style version

MGLStyleDefaultVersion is just for Streets now. Deleted style version documentation tests because not all styles are on the same version.

Cherry-picked from ca97dd8cf4.

* [ios, macos] Undeprecated unversioned style URL factory methods

Undeprecated the unversioned style URL factory methods in MGLStyle for consistency with the Android and Qt SDKs. Added warnings about using them with the runtime styling API.

Refactored mbgl::util::default_styles to track different versions for different styles.

Cherry-picked from 9e384b78fb.

* [core, android, ios, macos] Added Traffic Day/Night to default styles

The Styles API section of the Mapbox API Documentation site now lists Traffic Day v2 and Traffic Night v2, so this change adds those styles to all the places where styles are listed.

Also switched iosapp and macosapp to unversioned style factory methods since MGLStyleDefaultVersion is no longer applicable for all styles.

Cherry-picked from 4d6f54553d.

* [android] Release android-v5.1.0-beta.2 (#8976)

* [android] url getter on sources

* [android] fix ui test filter in makefile

* [android] - build SNAPSHOT from release branch (#8958)

* [android] - update changelog for 5.1.0-beta.2

* [android] - bump version number

* [android] - Camera change listener v2.0

* [core] allow filesource url transform reset

* [android] Update attribution wordmark (#8774)

* Update wordmark on android

* Moved attribution i icon to the right of mapbox word (in mapview preview image)

* update padding and margin

* [android] update hardcoded branch name

* revert version to 5.1.0-SNAPSHOT

* [android] - MarkerView deselect events with OnMarkerViewClickListener integration (#8996)

* [android] - publish SNAPSHOT from release 5.1.0 branch (#8995)

* [ios, macos] edited identity interpolation mode documentation (#8657)

* [ios, macos] Factored out tile URL template guide

Factored out redundant tile URL template documentation from the MGLRasterSource and MGLVectorSource documentation into a jazzy guide. This documentation used to live in one place, on a method on MGLTileSource, but that method had to be moved to MGLTileSource’s concrete subclasses. A jazzy guide is easier to link to, in any case.

* [ios] Telemetry button in modal view controllers (#9027)

Fixes #8980.

* [android] - bump tools and support lib version due to SNAPSHOT dependencies (#9046)

* [android] - bump tools and support lib version for SNAPSHOT dependencies

* revert unsupported Circle CI build tools version

* [core] Make destructor virtual to avoid object splicing during destruction

* [core] add error for non-virtual destructor deletes + add virtual dtors

* [android] -  remove marker from selected markers when removing marker from annotation manager. (#9047)

* Observe layout guides (#7716)

* [ios] observe layout guides

* [ios] update changelog

* [ios, macos] Change == to = in style function initializers

* [ios] Add annotation view initializer with annotation and reuse id (#9029)

* [ios] Remove annotation view from container view when annotation removed (#9025)

The annotation container view keeps an array of annotation views that is separate from the array of subviews that is a property of the UIView parent class. This removes an annotation view from that container view array when the associated annotation is removed. This avoids issue related to previously removed annotation views continuing to be involved in map view logic around annotation view selection due to taps.

* [android] - correct bearing conversion when animating the map with jumpTo, easeTo and animateTo. (#9050)

* [ios, macos] Updated `maximumZoomLevel` description, cherry-picked #8818 (#8842)

Cherry-picked from a3e4e67ea6.

* [ios] Updated podspecs and changelog for v3.5.3 (#8870)

Cherry-picked from 25c19902a2.

* [ios] Update podspecs and changelog for iOS v3.5.4

Cherry-picked from db7bb509e9.

* [ios, macos] Updated changelogs

* [ios, macos] changed TRUE -> 'true' (#9059) fixes #9056

* [ios, macos] Light property implementation in MGLStyle (#9043)

* [ios, macos] Add MGLLight to MGLStyle

* [ios, macos] Implement Objc bindings for Light object

* [ios, macos] Remove rawLight from MGLLight and re-implement it as value class

* [ios, macos] Fix build on macos

* [ios, macos] Add MGLLight documentation, Move MGLLightPosition to MGLLight

* [ios, macos] Add MGLLight tests.

* [ios, macos] Update changelogs

* [ios, macos] Fix misspelling

* [ios, macos] Fix MGLLightAnchor enum property names

* [ios, macos] Update documentation. Improve varialble naming.

* [ios, macos] Rename MGLLightPosition to MGLSphericalPosition

* [ios, macos] Update data types of MGLSphericalPosition

* [android] - horizontally rotated in snapshot (#9083)

* Cherry-pick arabic text to release branch v3.6.0 (#9071)

* [core] Throttle tiles to redo symbol placement at most once every 300ms.
Fixes issue #8435 and prepares for pitch-scaling changes in issue #8967.

* [core] Disable letter-spacing for Arabic labels (issue #9057)

* [ios,macos] multipolygon coordinate (#8713)

* [ios] MGLMultiPolygon's coordinate property implemented

* [ios,macos] Add polylabalel to project config

* [ios,macos] Change coordinate property for MGLPolyline,MGLPolygon,MGLMultiPolygon

* [macos] Change project configuration to support polilabel

* [ios,macos] Add MGLLocationCoordinate2DFromPoint

* [ios, macos] Update changelogs

* [ios, macos] remove unnecesary variables

* [ios, macos] Add radians conversions

* [ios, macos] Add coordinate calc to MGLPolyline

* [ios, macos] Move radian fuctions to MGLGeometry_Private.h

* [ios, macos] Fix code style

* [ios, macos] Fix code indentation

* [ios, macos] Fix radian per meters constant

* [ios, macos] Add test scenarios to testPolyline

* [ios, macos] Fix test accuracy

* [ios, macos] More robust Streets localization

Added Arabic, Portuguese, and Simplified Chinese to the list of languages with specialized fields in the Mapbox Streets source.

Rely on NSBundle to select the most appropriate locale based on the user’s preferred languages.

* [ios] Fallback to Mapbox.bundle as the framework bundle (#9074)

Fixes an issue where localizations could not be found when using static builds.

Throws exception if our bundle can't be found.

* [ios] Move image resources to an asset catalog & switch to PDFs

* [ios, macos] Updated changelogs

Reorganized the changelogs with a new packaging section. Added blurbs about #9062, #8713, #9060, and #9031.

* [ios] Update pods spec for iOS v3.6.0-beta.1

* [macos] Enable View ‣ Traffic Night

Enable the View ‣ Traffic Night menu item and check it when that style is active.

* [android] - stop location updates when toggle MyLocationView state (#9099)

* [android] - LatLngBounds bearing default value (#9102)

* [ios, macos] Fix MGLLight.achor to accept style functions

* [ios] Fix annotation initializers for subclasses of MGLAnnotationView (#9104)

Use a common init function in both of the provided initializers so that subclasses of `MGLAnnotationView` written in Swift don't need to override `init(annotation, reuseIdentifier)`

* [ios] Moved `MGLLight` in jazzy table of contents (#9111)

* [android] - logo placement for creating a Mapview programatically (#9094)

* [android] - Correct logo placement for creating a Mapview programatically, fixup non default placements

* Fixed NIGHTY_TWO_DP typo to NINETY_TWO_DP

* [android] - add binding support for Light (#9013)

* [android] Update Lost to final version 3.0.0 (#9112)

* update lost to final version 3.0.0

* bump MAS version to 2.1.1

* [android] - convert dp to pixels when getting meters per pixel at. (#9048)

* Release Android v5.1.0-beta.3 (#9115)

* [android] - Changelog update for Android release v5.1.0-beta.3

* update CI & version

* reset release properties

* [android] - harden orientation changes (#9128)

* [ios] Remove filter of single metric event

* [ios] remove layout guide observers

* Migrate to GL JS–powered feedback form (#9078)

* [ios, macos] Updated feedback URL

* [ios, macos] Add referrer, heading, pitch to feedback URL

* [ios, macos] Updated changelogs for feedback changes

* [ios] Vary referrer by platform

* [android] - javadoc update for 5.1.0 release (#9138)

* [android] - javadoc update for 5.1.0 release

* oxford comma

* Grammar/spelling tweaks

* grammar tweak

* grammar tweak

* Grammar tweaks

* Grammar tweaks

* [android] fix missing access token variable issue (#9151)

* [ios] Update pods spec for iOS v3.6.0-beta.2

* [android] Cherry picking 9133 (#9145)

* [android] Fix tracking mode + camera race condition (#9133)

* [android] fix tracking mode + camera race condition

* fix resetTrackingModesIfRequired bug (comparing current camera position with target camera position

* cherry pick #9133 and update CHANGELOG

* add null check to prevent null pointer exception

* add null check in custom location engine activity to prevent null pointer exception (#9159)

* [core] Make TransformState LatLngBounds optional

* [ios] Make annotation view rotation alignment configurable (#9147)

This commit adds `rotatesWithMap` property on `MGLAnnotationView`. This
property, when set to `YES` fixes the annotation to a map such that view
follows map's rotation angle. This is useful when user wants to display
rotation-dependent annotations (e.g. sector lights).

* [android] - validate if gestures should execute (#9173)
2017-06-02 23:41:14 +02:00

585 lines
20 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#import "MGLStyle_Private.h"
#import "MGLMapView_Private.h"
#import "MGLStyleLayer.h"
#import "MGLFillStyleLayer.h"
#import "MGLFillExtrusionStyleLayer.h"
#import "MGLLineStyleLayer.h"
#import "MGLCircleStyleLayer.h"
#import "MGLSymbolStyleLayer.h"
#import "MGLRasterStyleLayer.h"
#import "MGLBackgroundStyleLayer.h"
#import "MGLOpenGLStyleLayer.h"
#import "MGLStyle_Private.h"
#import "MGLStyleLayer_Private.h"
#import "MGLSource_Private.h"
#import "MGLLight_Private.h"
#import "NSDate+MGLAdditions.h"
#import "MGLSource.h"
#import "MGLTileSource_Private.h"
#import "MGLVectorSource.h"
#import "MGLRasterSource.h"
#import "MGLShapeSource.h"
#import "MGLAttributionInfo_Private.h"
#include <mbgl/map/map.hpp>
#include <mbgl/util/default_styles.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/style/light.hpp>
#include <mbgl/style/layers/fill_layer.hpp>
#include <mbgl/style/layers/fill_extrusion_layer.hpp>
#include <mbgl/style/layers/line_layer.hpp>
#include <mbgl/style/layers/symbol_layer.hpp>
#include <mbgl/style/layers/raster_layer.hpp>
#include <mbgl/style/layers/circle_layer.hpp>
#include <mbgl/style/layers/background_layer.hpp>
#include <mbgl/style/layers/custom_layer.hpp>
#include <mbgl/style/sources/geojson_source.hpp>
#include <mbgl/style/sources/vector_source.hpp>
#include <mbgl/style/sources/raster_source.hpp>
#if TARGET_OS_IPHONE
#import "UIImage+MGLAdditions.h"
#else
#import "NSImage+MGLAdditions.h"
#endif
@interface MGLStyle()
@property (nonatomic, readwrite, weak) MGLMapView *mapView;
@property (readonly, copy, nullable) NSURL *URL;
@property (nonatomic, readwrite, strong) NS_MUTABLE_DICTIONARY_OF(NSString *, MGLOpenGLStyleLayer *) *openGLLayers;
@end
@implementation MGLStyle
#pragma mark Default style URLs
/// @param name The style’s marketing name, written in lower camelCase.
/// @param fileName The last path component in the style’s URL, excluding the version suffix.
#define MGL_DEFINE_STYLE(name, fileName) \
static NSURL *MGLStyleURL_##name; \
+ (NSURL *)name##StyleURL { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
MGLStyleURL_##name = [self name##StyleURLWithVersion:mbgl::util::default_styles::name.currentVersion]; \
}); \
return MGLStyleURL_##name; \
} \
\
+ (NSURL *)name##StyleURL##WithVersion:(NSInteger)version { \
return [NSURL URLWithString:[@"mapbox://styles/mapbox/" #fileName "-v" stringByAppendingFormat:@"%li", (long)version]]; \
}
MGL_DEFINE_STYLE(streets, streets)
MGL_DEFINE_STYLE(outdoors, outdoors)
MGL_DEFINE_STYLE(light, light)
MGL_DEFINE_STYLE(dark, dark)
MGL_DEFINE_STYLE(satellite, satellite)
MGL_DEFINE_STYLE(satelliteStreets, satellite-streets)
MGL_DEFINE_STYLE(trafficDay, traffic-day)
MGL_DEFINE_STYLE(trafficNight, traffic-night)
// Make sure all the styles listed in mbgl::util::default_styles::orderedStyles
// are defined above and also declared in MGLStyle.h.
static_assert(8 == mbgl::util::default_styles::numOrderedStyles,
"mbgl::util::default_styles::orderedStyles and MGLStyle have different numbers of styles.");
// Hybrid has been renamed Satellite Streets, so the last Hybrid version is hard-coded here.
static NSURL *MGLStyleURL_hybrid;
+ (NSURL *)hybridStyleURL {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
MGLStyleURL_hybrid = [NSURL URLWithString:@"mapbox://styles/mapbox/satellite-hybrid-v8"];
});
return MGLStyleURL_hybrid;
}
// Emerald is no longer getting new versions as a default style, so the current version is hard-coded here.
static NSURL *MGLStyleURL_emerald;
+ (NSURL *)emeraldStyleURL {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
MGLStyleURL_emerald = [NSURL URLWithString:@"mapbox://styles/mapbox/emerald-v8"];
});
return MGLStyleURL_emerald;
}
#pragma mark -
- (instancetype)initWithMapView:(MGLMapView *)mapView {
if (self = [super init]) {
_mapView = mapView;
_openGLLayers = [NSMutableDictionary dictionary];
}
return self;
}
- (NSURL *)URL {
return [NSURL URLWithString:@(self.mapView.mbglMap->getStyleURL().c_str())];
}
- (NSString *)name {
std::string name = self.mapView.mbglMap->getStyleName();
return name.empty() ? nil : @(name.c_str());
}
#pragma mark Sources
- (NS_SET_OF(__kindof MGLSource *) *)sources {
auto rawSources = self.mapView.mbglMap->getSources();
NS_MUTABLE_SET_OF(__kindof MGLSource *) *sources = [NSMutableSet setWithCapacity:rawSources.size()];
for (auto rawSource = rawSources.begin(); rawSource != rawSources.end(); ++rawSource) {
MGLSource *source = [self sourceFromMBGLSource:*rawSource];
[sources addObject:source];
}
return sources;
}
- (void)setSources:(NS_SET_OF(__kindof MGLSource *) *)sources {
for (MGLSource *source in self.sources) {
[self removeSource:source];
}
for (MGLSource *source in sources) {
[self addSource:source];
}
}
- (NSUInteger)countOfSources {
auto rawSources = self.mapView.mbglMap->getSources();
return rawSources.size();
}
- (MGLSource *)memberOfSources:(MGLSource *)object {
return [self sourceWithIdentifier:object.identifier];
}
- (MGLSource *)sourceWithIdentifier:(NSString *)identifier
{
auto rawSource = self.mapView.mbglMap->getSource(identifier.UTF8String);
return rawSource ? [self sourceFromMBGLSource:rawSource] : nil;
}
- (MGLSource *)sourceFromMBGLSource:(mbgl::style::Source *)rawSource {
if (MGLSource *source = rawSource->peer.empty() ? nil : mbgl::any_cast<SourceWrapper>(rawSource->peer).source) {
return source;
}
// TODO: Fill in options specific to the respective source classes
// https://github.com/mapbox/mapbox-gl-native/issues/6584
if (auto vectorSource = rawSource->as<mbgl::style::VectorSource>()) {
return [[MGLVectorSource alloc] initWithRawSource:vectorSource];
} else if (auto geoJSONSource = rawSource->as<mbgl::style::GeoJSONSource>()) {
return [[MGLShapeSource alloc] initWithRawSource:geoJSONSource];
} else if (auto rasterSource = rawSource->as<mbgl::style::RasterSource>()) {
return [[MGLRasterSource alloc] initWithRawSource:rasterSource];
} else {
return [[MGLSource alloc] initWithRawSource:rawSource];
}
}
- (void)addSource:(MGLSource *)source
{
if (!source.rawSource) {
[NSException raise:NSInvalidArgumentException format:
@"The source %@ cannot be added to the style. "
@"Make sure the source was created as a member of a concrete subclass of MGLSource.",
source];
}
try {
[source addToMapView:self.mapView];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantSourceIdentifierException" format:@"%s", err.what()];
}
}
- (void)removeSource:(MGLSource *)source
{
if (!source.rawSource) {
[NSException raise:NSInvalidArgumentException format:
@"The source %@ cannot be removed from the style. "
@"Make sure the source was created as a member of a concrete subclass of MGLSource.",
source];
}
[source removeFromMapView:self.mapView];
}
- (nullable NS_ARRAY_OF(MGLAttributionInfo *) *)attributionInfosWithFontSize:(CGFloat)fontSize linkColor:(nullable MGLColor *)linkColor {
// It’d be incredibly convenient to use -sources here, but this operation
// depends on the sources being sorted in ascending order by creation, as
// with the std::vector used in mbgl.
auto rawSources = self.mapView.mbglMap->getSources();
NSMutableArray *infos = [NSMutableArray arrayWithCapacity:rawSources.size()];
for (auto rawSource = rawSources.begin(); rawSource != rawSources.end(); ++rawSource) {
MGLTileSource *source = (MGLTileSource *)[self sourceFromMBGLSource:*rawSource];
if (![source isKindOfClass:[MGLTileSource class]]) {
continue;
}
NSArray *tileSetInfos = [source attributionInfosWithFontSize:fontSize linkColor:linkColor];
[infos growArrayByAddingAttributionInfosFromArray:tileSetInfos];
}
return infos;
}
#pragma mark Style layers
- (NS_ARRAY_OF(__kindof MGLStyleLayer *) *)layers
{
auto layers = self.mapView.mbglMap->getLayers();
NS_MUTABLE_ARRAY_OF(__kindof MGLStyleLayer *) *styleLayers = [NSMutableArray arrayWithCapacity:layers.size()];
for (auto layer : layers) {
MGLStyleLayer *styleLayer = [self layerFromMBGLLayer:layer];
[styleLayers addObject:styleLayer];
}
return styleLayers;
}
- (void)setLayers:(NS_ARRAY_OF(__kindof MGLStyleLayer *) *)layers {
for (MGLStyleLayer *layer in self.layers) {
[self removeLayer:layer];
}
for (MGLStyleLayer *layer in layers) {
[self addLayer:layer];
}
}
- (NSUInteger)countOfLayers
{
return self.mapView.mbglMap->getLayers().size();
}
- (MGLStyleLayer *)objectInLayersAtIndex:(NSUInteger)index
{
auto layers = self.mapView.mbglMap->getLayers();
if (index >= layers.size()) {
[NSException raise:NSRangeException
format:@"No style layer at index %lu.", (unsigned long)index];
return nil;
}
auto layer = layers.at(index);
return [self layerFromMBGLLayer:layer];
}
- (void)getLayers:(MGLStyleLayer **)buffer range:(NSRange)inRange
{
auto layers = self.mapView.mbglMap->getLayers();
if (NSMaxRange(inRange) > layers.size()) {
[NSException raise:NSRangeException
format:@"Style layer range %@ is out of bounds.", NSStringFromRange(inRange)];
}
NSUInteger i = 0;
for (auto layer = *(layers.rbegin() + inRange.location); i < inRange.length; ++layer, ++i) {
MGLStyleLayer *styleLayer = [self layerFromMBGLLayer:layer];
buffer[i] = styleLayer;
}
}
- (void)insertObject:(MGLStyleLayer *)styleLayer inLayersAtIndex:(NSUInteger)index
{
if (!styleLayer.rawLayer) {
[NSException raise:NSInvalidArgumentException format:
@"The style layer %@ cannot be inserted into the style. "
@"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.",
styleLayer];
}
auto layers = self.mapView.mbglMap->getLayers();
if (index > layers.size()) {
[NSException raise:NSRangeException
format:@"Cannot insert style layer at out-of-bounds index %lu.", (unsigned long)index];
} else if (index == 0) {
try {
MGLStyleLayer *sibling = layers.size() ? [self layerFromMBGLLayer:layers.at(0)] : nil;
[styleLayer addToMapView:self.mapView belowLayer:sibling];
} catch (const std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
} else {
try {
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(index)];
[styleLayer addToMapView:self.mapView belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
}
}
- (void)removeObjectFromLayersAtIndex:(NSUInteger)index
{
auto layers = self.mapView.mbglMap->getLayers();
if (index >= layers.size()) {
[NSException raise:NSRangeException
format:@"Cannot remove style layer at out-of-bounds index %lu.", (unsigned long)index];
}
auto layer = layers.at(index);
MGLStyleLayer *styleLayer = [self layerFromMBGLLayer:layer];
[styleLayer removeFromMapView:self.mapView];
}
- (MGLStyleLayer *)layerFromMBGLLayer:(mbgl::style::Layer *)rawLayer
{
NSParameterAssert(rawLayer);
if (MGLStyleLayer *layer = rawLayer->peer.empty() ? nil : mbgl::any_cast<LayerWrapper>(rawLayer->peer).layer) {
return layer;
}
if (auto fillLayer = rawLayer->as<mbgl::style::FillLayer>()) {
return [[MGLFillStyleLayer alloc] initWithRawLayer:fillLayer];
} else if (auto fillExtrusionLayer = rawLayer->as<mbgl::style::FillExtrusionLayer>()) {
return [[MGLFillExtrusionStyleLayer alloc] initWithRawLayer:fillExtrusionLayer];
} else if (auto lineLayer = rawLayer->as<mbgl::style::LineLayer>()) {
return [[MGLLineStyleLayer alloc] initWithRawLayer:lineLayer];
} else if (auto symbolLayer = rawLayer->as<mbgl::style::SymbolLayer>()) {
return [[MGLSymbolStyleLayer alloc] initWithRawLayer:symbolLayer];
} else if (auto rasterLayer = rawLayer->as<mbgl::style::RasterLayer>()) {
return [[MGLRasterStyleLayer alloc] initWithRawLayer:rasterLayer];
} else if (auto circleLayer = rawLayer->as<mbgl::style::CircleLayer>()) {
return [[MGLCircleStyleLayer alloc] initWithRawLayer:circleLayer];
} else if (auto backgroundLayer = rawLayer->as<mbgl::style::BackgroundLayer>()) {
return [[MGLBackgroundStyleLayer alloc] initWithRawLayer:backgroundLayer];
} else if (auto customLayer = rawLayer->as<mbgl::style::CustomLayer>()) {
return [[MGLOpenGLStyleLayer alloc] initWithRawLayer:customLayer];
} else {
NSAssert(NO, @"Unrecognized layer type");
return nil;
}
}
- (MGLStyleLayer *)layerWithIdentifier:(NSString *)identifier
{
auto mbglLayer = self.mapView.mbglMap->getLayer(identifier.UTF8String);
return mbglLayer ? [self layerFromMBGLLayer:mbglLayer] : nil;
}
- (void)removeLayer:(MGLStyleLayer *)layer
{
if (!layer.rawLayer) {
[NSException raise:NSInvalidArgumentException format:
@"The style layer %@ cannot be removed from the style. "
@"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.",
layer];
}
[self willChangeValueForKey:@"layers"];
[layer removeFromMapView:self.mapView];
[self didChangeValueForKey:@"layers"];
}
- (void)addLayer:(MGLStyleLayer *)layer
{
if (!layer.rawLayer) {
[NSException raise:NSInvalidArgumentException format:
@"The style layer %@ cannot be added to the style. "
@"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.",
layer];
}
[self willChangeValueForKey:@"layers"];
try {
[layer addToMapView:self.mapView belowLayer:nil];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
[self didChangeValueForKey:@"layers"];
}
- (void)insertLayer:(MGLStyleLayer *)layer atIndex:(NSUInteger)index {
[self insertObject:layer inLayersAtIndex:index];
}
- (void)insertLayer:(MGLStyleLayer *)layer belowLayer:(MGLStyleLayer *)sibling
{
if (!layer.rawLayer) {
[NSException raise:NSInvalidArgumentException
format:
@"The style layer %@ cannot be added to the style. "
@"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.",
layer];
}
if (!sibling.rawLayer) {
[NSException raise:NSInvalidArgumentException
format:
@"A style layer cannot be placed below %@ in the style. "
@"Make sure sibling was obtained using -[MGLStyle layerWithIdentifier:].",
sibling];
}
[self willChangeValueForKey:@"layers"];
try {
[layer addToMapView:self.mapView belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
[self didChangeValueForKey:@"layers"];
}
- (void)insertLayer:(MGLStyleLayer *)layer aboveLayer:(MGLStyleLayer *)sibling {
if (!layer.rawLayer) {
[NSException raise:NSInvalidArgumentException
format:
@"The style layer %@ cannot be added to the style. "
@"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.",
layer];
}
if (!sibling.rawLayer) {
[NSException raise:NSInvalidArgumentException
format:
@"A style layer cannot be placed above %@ in the style. "
@"Make sure sibling was obtained using -[MGLStyle layerWithIdentifier:].",
sibling];
}
auto layers = self.mapView.mbglMap->getLayers();
std::string siblingIdentifier = sibling.identifier.UTF8String;
NSUInteger index = 0;
for (auto layer : layers) {
if (layer->getID() == siblingIdentifier) {
break;
}
index++;
}
[self willChangeValueForKey:@"layers"];
if (index + 1 > layers.size()) {
[NSException raise:NSInvalidArgumentException
format:
@"A style layer cannot be placed above %@ in the style. "
@"Make sure sibling was obtained using -[MGLStyle layerWithIdentifier:].",
sibling];
} else if (index + 1 == layers.size()) {
try {
[layer addToMapView:self.mapView belowLayer:nil];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
} else {
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(index + 1)];
try {
[layer addToMapView:self.mapView belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
}
[self didChangeValueForKey:@"layers"];
}
#pragma mark Style classes
- (NS_ARRAY_OF(NSString *) *)styleClasses
{
return @[];
}
- (void)setStyleClasses:(NS_ARRAY_OF(NSString *) *)appliedClasses
{
}
- (void)setStyleClasses:(NS_ARRAY_OF(NSString *) *)appliedClasses transitionDuration:(NSTimeInterval)transitionDuration
{
}
- (NSUInteger)countOfStyleClasses {
return 0;
}
- (BOOL)hasStyleClass:(NSString *)styleClass
{
return NO;
}
- (void)addStyleClass:(NSString *)styleClass
{
}
- (void)removeStyleClass:(NSString *)styleClass
{
}
#pragma mark Style images
- (void)setImage:(MGLImage *)image forName:(NSString *)name
{
if (!image) {
[NSException raise:NSInvalidArgumentException
format:@"Cannot assign nil image to “%@”.", name];
}
if (!name) {
[NSException raise:NSInvalidArgumentException
format:@"Cannot assign image %@ to a nil name.", image];
}
self.mapView.mbglMap->addImage([image mgl_styleImageWithIdentifier:name]);
}
- (void)removeImageForName:(NSString *)name
{
if (!name) {
[NSException raise:NSInvalidArgumentException
format:@"Cannot remove image with nil name."];
}
self.mapView.mbglMap->removeImage([name UTF8String]);
}
- (MGLImage *)imageForName:(NSString *)name
{
if (!name) {
[NSException raise:NSInvalidArgumentException
format:@"Cannot get image with nil name."];
}
auto styleImage = self.mapView.mbglMap->getImage([name UTF8String]);
return styleImage ? [[MGLImage alloc] initWithMGLStyleImage:styleImage] : nil;
}
#pragma mark Style transitions
- (void)setTransition:(MGLTransition)transition
{
auto transitionOptions = self.mapView.mbglMap->getTransitionOptions();
transitionOptions.duration = MGLDurationFromTimeInterval(transition.duration);
transitionOptions.delay = MGLDurationFromTimeInterval(transition.delay);
self.mapView.mbglMap->setTransitionOptions(transitionOptions);
}
- (MGLTransition)transition
{
MGLTransition transition;
const mbgl::style::TransitionOptions transitionOptions = self.mapView.mbglMap->getTransitionOptions();
transition.delay = MGLTimeIntervalFromDuration(transitionOptions.delay.value_or(mbgl::Duration::zero()));
transition.duration = MGLTimeIntervalFromDuration(transitionOptions.duration.value_or(mbgl::Duration::zero()));
return transition;
}
#pragma mark Style light
- (void)setLight:(MGLLight *)light
{
std::unique_ptr<mbgl::style::Light> mbglLight = std::make_unique<mbgl::style::Light>([light mbglLight]);
self.mapView.mbglMap->setLight(std::move(mbglLight));
}
- (MGLLight *)light
{
auto mbglLight = self.mapView.mbglMap->getLight();
MGLLight *light = [[MGLLight alloc] initWithMBGLLight:mbglLight];
return light;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p; name = %@, URL = %@>",
NSStringFromClass([self class]), (void *)self,
self.name ? [NSString stringWithFormat:@"\"%@\"", self.name] : self.name,
self.URL ? [NSString stringWithFormat:@"\"%@\"", self.URL] : self.URL];
}
@end