[ios] Custom style layer

Exposed Map::addCustomLayer() publicly as an undocumented, block-based API. This change avoids the terms “layer”, “initialize”, “render”, and “altitude” because they overload existing terminology in the SDK, UIKit, or MapKit. (“Layer” is qualified as “style layer”.) There are outstanding questions about ARC memory management, but this approach works in both Objective-C and Swift for pure OpenGL programming.

Added a demo to iosapp that draws a green layer below any labels in Mapbox Streets.
This commit is contained in:
Minh Nguyễn
2015-12-16 09:29:54 -08:00
committed by John Firebaugh
parent 8a17d30c50
commit ec9a4cd328
5 changed files with 149 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
#import "MGLMapView.h"
#import "MGLMapView+IBAdditions.h"
#import "MGLMapView+MGLCustomStyleLayerAdditions.h"
#import <mbgl/platform/log.hpp>
#import <mbgl/platform/gl.hpp>
@@ -3472,6 +3473,8 @@ class MBGLView : public mbgl::View
@end
#pragma mark - IBAdditions methods
@implementation MGLMapView (IBAdditions)
+ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingStyleURL__
@@ -3610,3 +3613,66 @@ class MBGLView : public mbgl::View
}
@end
#pragma mark - MGLCustomStyleLayerAdditions methods
class MGLCustomStyleLayerHandlers
{
public:
MGLCustomStyleLayerHandlers(MGLCustomStyleLayerPreparationHandler p,
MGLCustomStyleLayerDrawingHandler d,
MGLCustomStyleLayerCompletionHandler f)
: prepare(p), draw(d), finish(f) {}
MGLCustomStyleLayerPreparationHandler prepare;
MGLCustomStyleLayerDrawingHandler draw;
MGLCustomStyleLayerCompletionHandler finish;
};
void MGLPrepareCustomStyleLayer(void *context)
{
MGLCustomStyleLayerPreparationHandler prepare = reinterpret_cast<MGLCustomStyleLayerHandlers *>(context)->prepare;
if (prepare)
{
prepare();
}
}
void MGLDrawCustomStyleLayer(void *context, const mbgl::CustomLayerRenderParameters &params)
{
CGSize size = CGSizeMake(params.width, params.height);
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(params.latitude, params.longitude);
double zoomLevel = params.zoom;
CLLocationDirection direction = mbgl::util::wrap(params.bearing, 0., 360.);
CGFloat pitch = params.pitch;
CGFloat perspectiveSkew = params.altitude;
MGLCustomStyleLayerDrawingHandler draw = reinterpret_cast<MGLCustomStyleLayerHandlers *>(context)->draw;
if (draw)
{
draw(size, centerCoordinate, zoomLevel, direction, pitch, perspectiveSkew);
}
}
void MGLFinishCustomStyleLayer(void *context)
{
MGLCustomStyleLayerHandlers *handlers = reinterpret_cast<MGLCustomStyleLayerHandlers *>(context);
MGLCustomStyleLayerCompletionHandler finish = handlers->finish;
if (finish)
{
finish();
}
delete handlers;
}
@implementation MGLMapView (MGLCustomStyleLayerAdditions)
- (void)insertCustomStyleLayerWithIdentifier:(NSString *)identifier preparationHandler:(void (^)())preparation drawingHandler:(MGLCustomStyleLayerDrawingHandler)drawing completionHandler:(void (^)())completion belowStyleLayerWithIdentifier:(nullable NSString *)otherIdentifier
{
NSAssert(identifier, @"Style layer needs an identifier");
MGLCustomStyleLayerHandlers *context = new MGLCustomStyleLayerHandlers(preparation, drawing, completion);
_mbglMap->addCustomLayer(identifier.UTF8String, MGLPrepareCustomStyleLayer,
MGLDrawCustomStyleLayer, MGLFinishCustomStyleLayer,
context, otherIdentifier.UTF8String);
}
@end