diff options
author | Roman Blum <rmnblm@gmail.com> | 2016-10-12 21:30:20 +0200 |
---|---|---|
committer | Minh Nguyễn <mxn@1ec5.org> | 2016-10-12 12:30:20 -0700 |
commit | a24e559ac83a47d19e28290c7ded427adeb303a3 (patch) | |
tree | 930c91599bea3b6a524559ba900fb42b8b034f98 /platform/darwin/src/MGLStyle.mm | |
parent | 7bbf4a286e36ac26e7fa7317bd7942b5163c1188 (diff) | |
download | qtlocation-mapboxgl-a24e559ac83a47d19e28290c7ded427adeb303a3.tar.gz |
[ios, macos] possibility to set custom images to style (#6637)
* [ios, macos] possibility to set custom images to style
* [ios, macos] setImage:forName now supports MGLImage (UIImage/NSImage)
* [ios, macos] update documentation for setImage:forName: and removeImageForName:
* [ios, macos] rename method and fix prefix
* [ios, macos] change header visibility to project
* [ios, macos] update header imports
* [ios, macos] delete unnecessary whitespaces
* [ios, macos] remove unnecessary nil checks
* [ios, macos] delete unnecessary whitespaces
* [ios, macos] update documentation
* [ios, macos] make mgl_spriteImage a parameter-less instance method
* [ios, macos] add asserts
Diffstat (limited to 'platform/darwin/src/MGLStyle.mm')
-rw-r--r-- | platform/darwin/src/MGLStyle.mm | 48 |
1 files changed, 35 insertions, 13 deletions
diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm index f1c3e68e3a..f9f2e03d40 100644 --- a/platform/darwin/src/MGLStyle.mm +++ b/platform/darwin/src/MGLStyle.mm @@ -21,6 +21,7 @@ #import "MGLGeoJSONSource.h" #include <mbgl/util/default_styles.hpp> +#include <mbgl/sprite/sprite_image.hpp> #include <mbgl/style/layers/fill_layer.hpp> #include <mbgl/style/layers/line_layer.hpp> #include <mbgl/style/layers/symbol_layer.hpp> @@ -32,6 +33,12 @@ #include <mbgl/style/sources/raster_source.hpp> #include <mbgl/mbgl.hpp> +#if TARGET_OS_IPHONE + #import "UIImage+MGLAdditions.h" +#else + #import "NSImage+MGLAdditions.h" +#endif + @interface MGLStyle() @property (nonatomic, weak) MGLMapView *mapView; @property (readonly, copy, nullable) NSURL *URL; @@ -103,7 +110,7 @@ static NSURL *MGLStyleURL_emerald; if (!mbglLayer) { return nil; } - + MGLStyleLayer *styleLayer; if (auto fillLayer = mbglLayer->as<mbgl::style::FillLayer>()) { MGLSource *source = [self sourceWithIdentifier:@(fillLayer->getSourceID().c_str())]; @@ -126,9 +133,9 @@ static NSURL *MGLStyleURL_emerald; NSAssert(NO, @"Unrecognized layer type"); return nil; } - + styleLayer.layer = mbglLayer; - + return styleLayer; } @@ -138,7 +145,7 @@ static NSURL *MGLStyleURL_emerald; if (!mbglSource) { return nil; } - + // TODO: Fill in options specific to the respective source classes // https://github.com/mapbox/mapbox-gl-native/issues/6584 MGLSource *source; @@ -152,9 +159,9 @@ static NSURL *MGLStyleURL_emerald; NSAssert(NO, @"Unrecognized source type"); return nil; } - + source.source = mbglSource; - + return source; } @@ -171,7 +178,7 @@ static NSURL *MGLStyleURL_emerald; @"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.", NSStringFromClass(self)]; } - + self.mapView.mbglMap->addLayer(std::unique_ptr<mbgl::style::Layer>(layer.layer)); } @@ -191,7 +198,7 @@ static NSURL *MGLStyleURL_emerald; @"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.", NSStringFromClass(otherLayer)]; } - + const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String}; self.mapView.mbglMap->addLayer(std::unique_ptr<mbgl::style::Layer>(layer.layer), belowLayerId); } @@ -209,13 +216,13 @@ static NSURL *MGLStyleURL_emerald; - (NS_ARRAY_OF(NSString *) *)styleClasses { const std::vector<std::string> &appliedClasses = self.mapView.mbglMap->getClasses(); - + NSMutableArray *returnArray = [NSMutableArray arrayWithCapacity:appliedClasses.size()]; - + for (auto appliedClass : appliedClasses) { [returnArray addObject:@(appliedClass.c_str())]; } - + return returnArray; } @@ -227,12 +234,12 @@ static NSURL *MGLStyleURL_emerald; - (void)setStyleClasses:(NS_ARRAY_OF(NSString *) *)appliedClasses transitionDuration:(NSTimeInterval)transitionDuration { std::vector<std::string> newAppliedClasses; - + for (NSString *appliedClass in appliedClasses) { newAppliedClasses.push_back([appliedClass UTF8String]); } - + mbgl::style::TransitionOptions transition { { MGLDurationInSeconds(transitionDuration) } }; self.mapView.mbglMap->setTransitionOptions(transition); self.mapView.mbglMap->setClasses(newAppliedClasses); @@ -259,6 +266,21 @@ static NSURL *MGLStyleURL_emerald; } } +- (void)setImage:(MGLImage *)image forName:(NSString *)name +{ + NSAssert(image, @"image is null"); + NSAssert(name, @"name is null"); + + self.mapView.mbglMap->addImage([name UTF8String], image.mgl_spriteImage); +} + +- (void)removeImageForName:(NSString *)name +{ + NSAssert(name, @"name is null"); + + self.mapView.mbglMap->removeImage([name UTF8String]); +} + - (NSString *)description { return [NSString stringWithFormat:@"<%@: %p; name = %@, URL = %@>", |