diff options
author | Asheem Mamoowala <asheem.mamoowala@mapbox.com> | 2017-05-25 14:36:05 -0700 |
---|---|---|
committer | Asheem Mamoowala <asheem.mamoowala@mapbox.com> | 2017-06-19 09:50:08 -0700 |
commit | ab5b310a9eb7c88935cc457da1af81349fbe8d41 (patch) | |
tree | e1485c8aa2f00e2b50daa0aa7f997ddda59c2562 /platform/darwin | |
parent | 7b05606464ac4d57b59b64015629e9578cbebac2 (diff) | |
download | qtlocation-mapboxgl-ab5b310a9eb7c88935cc457da1af81349fbe8d41.tar.gz |
[iOS][macOS] Add ImageSource bindings
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/src/MGLGeometry.h | 17 | ||||
-rw-r--r-- | platform/darwin/src/MGLGeometry_Private.h | 22 | ||||
-rw-r--r-- | platform/darwin/src/MGLImageSource.h | 135 | ||||
-rw-r--r-- | platform/darwin/src/MGLImageSource.mm | 105 | ||||
-rw-r--r-- | platform/darwin/src/MGLImageSource_Private.h | 9 | ||||
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage.h | 2 | ||||
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage.mm | 3 | ||||
-rw-r--r-- | platform/darwin/src/MGLStyle.mm | 4 | ||||
-rw-r--r-- | platform/darwin/src/NSValue+MGLAdditions.h | 14 | ||||
-rw-r--r-- | platform/darwin/src/NSValue+MGLAdditions.m | 10 | ||||
-rw-r--r-- | platform/darwin/test/MGLDocumentationExampleTests.swift | 14 | ||||
-rw-r--r-- | platform/darwin/test/MGLGeometryTests.mm | 19 | ||||
-rw-r--r-- | platform/darwin/test/MGLImageSourceTests.m | 42 | ||||
-rw-r--r-- | platform/darwin/test/MGLStyleTests.mm | 18 | ||||
-rw-r--r-- | platform/darwin/test/Media.xcassets/RadarImage.imageset/Contents.json | 21 | ||||
-rw-r--r-- | platform/darwin/test/Media.xcassets/RadarImage.imageset/radar.png | bin | 0 -> 44094 bytes |
16 files changed, 426 insertions, 9 deletions
diff --git a/platform/darwin/src/MGLGeometry.h b/platform/darwin/src/MGLGeometry.h index 9fcb9dd37c..003a7638e7 100644 --- a/platform/darwin/src/MGLGeometry.h +++ b/platform/darwin/src/MGLGeometry.h @@ -45,6 +45,23 @@ typedef struct MGLCoordinateBounds { CLLocationCoordinate2D ne; } MGLCoordinateBounds; +/** + A quadrilateral area as measured on a two-dimensional map projection. + `MGLCoordinateQuad` differs from `MGLCoordinateBounds` in that it allows + representation of non-axis aligned bounds and non-rectangular quadrilaterals + */ +typedef struct MGLCoordinateQuad { + /** Coordinate at the top left corner. */ + CLLocationCoordinate2D topLeft; + /** Coordinate at the top right corner. */ + CLLocationCoordinate2D topRight; + /** Coordinate at the bottom right corner. */ + CLLocationCoordinate2D bottomRight; + /** Coordinate at the bottom left corner. */ + CLLocationCoordinate2D bottomLeft; +} MGLCoordinateQuad; + + /** Creates a new `MGLCoordinateBounds` structure from the given southwest and northeast coordinates. diff --git a/platform/darwin/src/MGLGeometry_Private.h b/platform/darwin/src/MGLGeometry_Private.h index 7ad8314a79..bdc838667a 100644 --- a/platform/darwin/src/MGLGeometry_Private.h +++ b/platform/darwin/src/MGLGeometry_Private.h @@ -8,6 +8,7 @@ #import <mbgl/util/geo.hpp> #import <mbgl/util/geometry.hpp> +#import <array> typedef double MGLLocationRadians; typedef double MGLRadianDistance; typedef double MGLRadianDirection; @@ -56,6 +57,27 @@ NS_INLINE mbgl::LatLngBounds MGLLatLngBoundsFromCoordinateBounds(MGLCoordinateBo MGLLatLngFromLocationCoordinate2D(coordinateBounds.ne)); } +NS_INLINE std::array<mbgl::LatLng, 4> MGLLatLngArrayFromCoordinateQuad(MGLCoordinateQuad quad) { + return { MGLLatLngFromLocationCoordinate2D(quad.topLeft), + MGLLatLngFromLocationCoordinate2D(quad.topRight), + MGLLatLngFromLocationCoordinate2D(quad.bottomRight), + MGLLatLngFromLocationCoordinate2D(quad.bottomLeft) }; +} + +NS_INLINE MGLCoordinateQuad MGLCoordinateQuadFromLatLngArray(std::array<mbgl::LatLng, 4> quad) { + return { MGLLocationCoordinate2DFromLatLng(quad[0]), + MGLLocationCoordinate2DFromLatLng(quad[1]), + MGLLocationCoordinate2DFromLatLng(quad[2]), + MGLLocationCoordinate2DFromLatLng(quad[3]) }; +} + +NS_INLINE MGLCoordinateQuad MGLCoordinateQuadFromCoordinateBounds(MGLCoordinateBounds bounds) { + return { { bounds.ne.latitude, bounds.sw.longitude }, + bounds.ne, + { bounds.sw.latitude, bounds.ne.longitude }, + bounds.sw }; +} + #if TARGET_OS_IPHONE NS_INLINE mbgl::EdgeInsets MGLEdgeInsetsFromNSEdgeInsets(UIEdgeInsets insets) { return { insets.top, insets.left, insets.bottom, insets.right }; diff --git a/platform/darwin/src/MGLImageSource.h b/platform/darwin/src/MGLImageSource.h new file mode 100644 index 0000000000..9ef2c6719a --- /dev/null +++ b/platform/darwin/src/MGLImageSource.h @@ -0,0 +1,135 @@ +#import "MGLSource.h" + +#import "MGLFoundation.h" +#import "MGLTypes.h" +#import "MGLGeometry.h" + +NS_ASSUME_NONNULL_BEGIN + +MGL_EXPORT +/** + `MGLImageSource` is a map content source that supplies a georeferenced raster + image to be shown on the map. The geographic location of the raster image content, + supplied with `MGLCoordinateQuad`, can be non-axis aligned. + `MGLImageSource` supports raster content from `NSURL` or an `MGLImage`. + Supported image formats are : PNG, TIFF, JPEG, GIF (single frame rendering), + and BMP. + An image source is added to an `MGLStyle` object along with one or more + `MGLRasterStyleLayer` objects. Use a raster style layer to control the + appearance of content supplied by the image source. + + Each + <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-image"><code>image</code></a> + source defined by the style JSON file is represented at runtime by an + `MGLImageSource` object that you can use to initialize new style layers. You + can also add and remove sources dynamically using methods such as + `-[MGLStyle addSource:]` and `-[MGLStyle sourceWithIdentifier:]`. + + ### Example + + ```swift + let coordinates: MGLCoordinateQuad = MGLCoordinateQuad( + topLeft: CLLocationCoordinate2D(latitude: 46.437, longitude: -80.425), + topRight: CLLocationCoordinate2D(latitude: 46.437, longitude: -71.516), + bottomRight: CLLocationCoordinate2D(latitude: 37.936, longitude: -71.516), + bottomLeft: CLLocationCoordinate2D(latitude: 37.936, longitude: -80.425)) + let source = MGLImageSource(identifier: "images", coordinates: coordinates, imageURL: URL(string: "https://www.mapbox.com/mapbox-gl-js/assets/radar.gif")!) + mapView.style?.addSource(source) + ``` + */ +@interface MGLImageSource : MGLSource + +#pragma mark Initializing a Source + +/** + Returns a georeferenced image source with an identifier and coordinates + + @param identifier A string that uniquely identifies the source. + @param coordinateQuad The top left, top right, bottom right, and bottom left coordinates for the image. + @return An initialized image source. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier coordinates:(MGLCoordinateQuad)coordinateQuad NS_DESIGNATED_INITIALIZER; + +/** + Returns a georeferenced image source with an identifier, coordinates and a URL + + @param identifier A string that uniquely identifies the source. + @param coordinateQuad the top left, top right, bottom right, and bottom left coordinates for the image. + @param url An HTTP(S) URL, absolute file URL, or local file URL relative to the + current application’s resource bundle. + @return An initialized shape source. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier coordinates:(MGLCoordinateQuad)coordinateQuad imageURL:(NSURL *)url; + +/** + Returns a georeferenced image source with an identifier, coordinates and an image + + @param identifier A string that uniquely identifies the source. + @param coordinateQuad The top left, top right, bottom right, and bottom left coordinates for the image. + @param image The image to display for the sourcde. + @return An initialized shape source. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier coordinates:(MGLCoordinateQuad)coordinateQuad image:(MGLImage *)image; + +/** + Returns a georeferenced image source with an identifier and coordinates + + @param identifier A string that uniquely identifies the source. + @param bounds The aligned coordinate bounds for the image. + @return An initialized image source. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier bounds:(MGLCoordinateBounds)bounds; + +/** + Returns a georeferenced image source with an identifier, coordinates and a URL + + @param identifier A string that uniquely identifies the source. + @param bounds The aligned coordinate bounds for the image.@param url An HTTP(S) URL, absolute file URL, or local file URL relative to the + current application’s resource bundle. + @return An initialized shape source. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier bounds:(MGLCoordinateBounds)bounds imageURL:(NSURL *)url; + +/** + Returns a georeferenced image source with an identifier, coordinates and an image + + @param identifier A string that uniquely identifies the source. + @param bounds The aligned coordinate bounds for the image. + @param image The image to display for the sourcde. + @return An initialized shape source. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier bounds:(MGLCoordinateBounds)bounds image:(MGLImage *)image; + +#pragma mark Accessing a Source’s Content + +/** + The URL to the source image. + + If the receiver was initialized using `-initWithIdentifier:coordinates:` or + `-initWithIdentifier:coordinates:image:`, or the `image` property is set, + this property is set to `nil`. + */ +@property (nonatomic, copy, nullable)NSURL *URL; + +/** + The source image. + + If the receiver was initialized using `-initWithIdentifier:coordinates:` or + `-initWithIdentifier:coordinates:imageURL:`, or if the `URL` property is set, + this property is set to `nil`. + + + */ +@property (nonatomic, retain, nullable)MGLImage *image; + +/** + The coordinates at which the source image will be placed. + + If the receiver was initialized using `-initWithIdentifier:bounds:`, + `-initWithIdentifier:bounds:image:`, or `initWithIdentifier:bounds:imageURL`, + the bounds will be converted to an `MGLCoordinateQuad`. + */ +@property (nonatomic) MGLCoordinateQuad coordinates; +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLImageSource.mm b/platform/darwin/src/MGLImageSource.mm new file mode 100644 index 0000000000..9872a65aff --- /dev/null +++ b/platform/darwin/src/MGLImageSource.mm @@ -0,0 +1,105 @@ +#import "MGLImageSource_Private.h" + +#import "MGLGeometry_Private.h" +#import "MGLSource_Private.h" +#import "MGLTileSource_Private.h" +#import "NSURL+MGLAdditions.h" +#if TARGET_OS_IPHONE + #import "UIImage+MGLAdditions.h" +#else + #import "NSImage+MGLAdditions.h" +#endif + +#include <mbgl/style/sources/image_source.hpp> +#include <mbgl/util/premultiply.hpp> + +@interface MGLImageSource () + +@property (nonatomic, readonly) mbgl::style::ImageSource *rawSource; + +@end + +@implementation MGLImageSource + +- (instancetype)initWithIdentifier:(NSString *)identifier coordinates:(MGLCoordinateQuad)coordinateQuad { + + const auto coordsArray = MGLLatLngArrayFromCoordinateQuad(coordinateQuad); + auto source = std::make_unique<mbgl::style::ImageSource>(identifier.UTF8String, coordsArray); + return self = [super initWithPendingSource:std::move(source)]; +} + + +- (instancetype)initWithIdentifier:(NSString *)identifier coordinates:(MGLCoordinateQuad)coordinateQuad imageURL:(NSURL *)url { + self = [self initWithIdentifier:identifier coordinates: coordinateQuad]; + self.URL = url; + return self; +} + + +- (instancetype)initWithIdentifier:(NSString *)identifier coordinates:(MGLCoordinateQuad)coordinateQuad image:(MGLImage *)image { + self = [self initWithIdentifier:identifier coordinates: coordinateQuad]; + self.image = image; + + return self; +} + +- (instancetype)initWithIdentifier:(NSString *)identifier bounds:(MGLCoordinateBounds)bounds { + self = [self initWithIdentifier:identifier coordinates:MGLCoordinateQuadFromCoordinateBounds(bounds)]; + return self; +} + +- (instancetype)initWithIdentifier:(NSString *)identifier bounds:(MGLCoordinateBounds)bounds imageURL:(NSURL *)url { + self = [self initWithIdentifier:identifier coordinates:MGLCoordinateQuadFromCoordinateBounds(bounds) imageURL:url]; + return self; +} + +- (instancetype)initWithIdentifier:(NSString *)identifier bounds:(MGLCoordinateBounds)bounds image:(MGLImage *)image { + self = [self initWithIdentifier:identifier coordinates:MGLCoordinateQuadFromCoordinateBounds(bounds) image:image]; + return self; +} + +- (NSURL *)URL { + auto url = self.rawSource->getURL(); + return url ? [NSURL URLWithString:@(url->c_str())] : nil; +} + +- (void)setURL:(NSURL *)url { + if (url) { + self.rawSource->setURL(url.mgl_URLByStandardizingScheme.absoluteString.UTF8String); + _image = nil; + } else { + self.image = nullptr; + } +} + +- (void)setImage:(MGLImage *)image { + if (image != nullptr) { + mbgl::UnassociatedImage unassociatedImage = mbgl::util::unpremultiply(image.mgl_PremultipliedImage); + self.rawSource->setImage(std::move(unassociatedImage)); + } + _image = image; +} + +- (MGLCoordinateQuad)coordinates { + return MGLCoordinateQuadFromLatLngArray(self.rawSource->getCoordinates()); +} + +- (void)setCoordinates: (MGLCoordinateQuad)coordinateQuad { + self.rawSource->setCoordinates(MGLLatLngArrayFromCoordinateQuad(coordinateQuad)); +} + +- (NSString *)description { + return [NSString stringWithFormat:@"<%@: %p; identifier = %@; URL = %@; image = %@>", + NSStringFromClass([self class]), (void *)self, self.identifier, self.URL, self.image]; +} + +- (mbgl::style::ImageSource *)rawSource { + return (mbgl::style::ImageSource *)super.rawSource; +} + +- (NSString *)attributionHTMLString { + auto attribution = self.rawSource->getAttribution(); + return attribution ? @(attribution->c_str()) : nil; +} + +@end diff --git a/platform/darwin/src/MGLImageSource_Private.h b/platform/darwin/src/MGLImageSource_Private.h new file mode 100644 index 0000000000..54f6e00afa --- /dev/null +++ b/platform/darwin/src/MGLImageSource_Private.h @@ -0,0 +1,9 @@ +#import "MGLImageSource.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MGLImageSource (Private) + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLOfflineStorage.h b/platform/darwin/src/MGLOfflineStorage.h index 16f134adb1..90ba15c84c 100644 --- a/platform/darwin/src/MGLOfflineStorage.h +++ b/platform/darwin/src/MGLOfflineStorage.h @@ -154,6 +154,8 @@ typedef NS_ENUM(NSUInteger, MGLResourceKind) { /** JSON part of a sprite sheet. It is constructed of the prefix in https://www.mapbox.com/mapbox-gl-js/style-spec/#root-sprite and a JSON file extension. */ MGLResourceKindSpriteJSON, + /** Image **/ + MGLResourceKindImage, }; /** diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index 81774ad3cb..040b36f8be 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -99,6 +99,9 @@ NSString * const MGLOfflinePackMaximumCountUserInfoKey = MGLOfflinePackUserInfoK case mbgl::Resource::Kind::SpriteJSON: kind = MGLResourceKindSpriteJSON; break; + case mbgl::Resource::Kind::Image: + kind = MGLResourceKindImage; + break; case mbgl::Resource::Kind::Unknown: kind = MGLResourceKindUnknown; break; diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm index 2e6f2bc29a..592ab86780 100644 --- a/platform/darwin/src/MGLStyle.mm +++ b/platform/darwin/src/MGLStyle.mm @@ -23,6 +23,7 @@ #import "MGLVectorSource.h" #import "MGLRasterSource.h" #import "MGLShapeSource.h" +#import "MGLImageSource.h" #import "MGLAttributionInfo_Private.h" @@ -41,6 +42,7 @@ #include <mbgl/style/sources/geojson_source.hpp> #include <mbgl/style/sources/vector_source.hpp> #include <mbgl/style/sources/raster_source.hpp> +#include <mbgl/style/sources/image_source.hpp> #if TARGET_OS_IPHONE #import "UIImage+MGLAdditions.h" @@ -178,6 +180,8 @@ static NSURL *MGLStyleURL_emerald; return [[MGLShapeSource alloc] initWithRawSource:geoJSONSource]; } else if (auto rasterSource = rawSource->as<mbgl::style::RasterSource>()) { return [[MGLRasterSource alloc] initWithRawSource:rasterSource]; + } else if (auto imageSource = rawSource->as<mbgl::style::ImageSource>()) { + return [[MGLImageSource alloc] initWithRawSource:imageSource]; } else { return [[MGLSource alloc] initWithRawSource:rawSource]; } diff --git a/platform/darwin/src/NSValue+MGLAdditions.h b/platform/darwin/src/NSValue+MGLAdditions.h index 0aaa2a337a..f3026a389f 100644 --- a/platform/darwin/src/NSValue+MGLAdditions.h +++ b/platform/darwin/src/NSValue+MGLAdditions.h @@ -56,6 +56,20 @@ NS_ASSUME_NONNULL_BEGIN */ @property (readonly) MGLCoordinateBounds MGLCoordinateBoundsValue; +/** + Creates a new value object containing the specified Mapbox coordinate + quad structure. + + @param quad The value for the new object. + @return A new value object that contains the coordinate quad information. + */ ++ (instancetype)valueWithMGLCoordinateQuad:(MGLCoordinateQuad)quad; + +/** + The Mapbox coordinate quad structure representation of the value. + */ +- (MGLCoordinateQuad)MGLCoordinateQuadValue; + #pragma mark Working with Offline Map Values /** diff --git a/platform/darwin/src/NSValue+MGLAdditions.m b/platform/darwin/src/NSValue+MGLAdditions.m index ef894f0eb4..1383056944 100644 --- a/platform/darwin/src/NSValue+MGLAdditions.m +++ b/platform/darwin/src/NSValue+MGLAdditions.m @@ -34,6 +34,16 @@ return bounds; } ++ (instancetype)valueWithMGLCoordinateQuad:(MGLCoordinateQuad)quad { + return [self valueWithBytes:&quad objCType:@encode(MGLCoordinateQuad)]; +} + +- (MGLCoordinateQuad)MGLCoordinateQuadValue { + MGLCoordinateQuad quad; + [self getValue:&quad]; + return quad; +} + #pragma mark Offline maps + (NSValue *)valueWithMGLOfflinePackProgress:(MGLOfflinePackProgress)progress { diff --git a/platform/darwin/test/MGLDocumentationExampleTests.swift b/platform/darwin/test/MGLDocumentationExampleTests.swift index 48e6b17f44..2a441d2774 100644 --- a/platform/darwin/test/MGLDocumentationExampleTests.swift +++ b/platform/darwin/test/MGLDocumentationExampleTests.swift @@ -104,6 +104,20 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate { XCTAssertNotNil(mapView.style?.source(withIdentifier: "pois")) } + func testMGLImageSource() { + //#-example-code + let coordinates: MGLCoordinateQuad = MGLCoordinateQuad( + topLeft: CLLocationCoordinate2D(latitude: 46.437, longitude: -80.425), + topRight: CLLocationCoordinate2D(latitude: 46.437, longitude: -71.516), + bottomRight: CLLocationCoordinate2D(latitude: 37.936, longitude: -71.516), + bottomLeft: CLLocationCoordinate2D(latitude: 37.936, longitude: -80.425)) + let source = MGLImageSource(identifier: "images", coordinates: coordinates, imageURL: URL(string: "https://www.mapbox.com/mapbox-gl-js/assets/radar.gif")!) + mapView.style?.addSource(source) + //#-end-example-code + + XCTAssertNotNil(mapView.style?.source(withIdentifier: "images")) + } + func testMGLCircleStyleLayer() { let population = MGLVectorSource(identifier: "population", configurationURL: URL(string: "https://example.com/style.json")!) mapView.style?.addSource(population) diff --git a/platform/darwin/test/MGLGeometryTests.mm b/platform/darwin/test/MGLGeometryTests.mm index 220a837643..1c85470188 100644 --- a/platform/darwin/test/MGLGeometryTests.mm +++ b/platform/darwin/test/MGLGeometryTests.mm @@ -144,4 +144,23 @@ XCTAssertEqualObjects(serializedGeoJSON, geoJSON, @"MGLPointFeature should serialize as a GeoJSON point feature."); } +- (void)testMGLCoordinateBoundsToMGLCoordinateQuad { + MGLCoordinateBounds bounds = MGLCoordinateBoundsMake(CLLocationCoordinate2DMake(37.936, -80.425), + CLLocationCoordinate2DMake(46.437, -71.516)); + + MGLCoordinateQuad quad = MGLCoordinateQuadFromCoordinateBounds(bounds); + XCTAssertEqualObjects([NSValue valueWithMGLCoordinate:bounds.sw], + [NSValue valueWithMGLCoordinate:quad.bottomLeft], + @"Bounds southwest should be bottom left of quad."); + XCTAssertEqualObjects([NSValue valueWithMGLCoordinate:bounds.ne], + [NSValue valueWithMGLCoordinate:quad.topRight], + @"Bounds northeast should be top right of quad."); + + XCTAssertEqualObjects([NSValue valueWithMGLCoordinate:CLLocationCoordinate2DMake(46.437, -80.425)], + [NSValue valueWithMGLCoordinate:quad.topLeft], + @"Quad top left should be computed correctly."); + XCTAssertEqualObjects([NSValue valueWithMGLCoordinate:CLLocationCoordinate2DMake(37.936, -71.516)], + [NSValue valueWithMGLCoordinate:quad.bottomRight], + @"Quad bottom right should be computed correctly."); +} @end diff --git a/platform/darwin/test/MGLImageSourceTests.m b/platform/darwin/test/MGLImageSourceTests.m new file mode 100644 index 0000000000..3349a7463b --- /dev/null +++ b/platform/darwin/test/MGLImageSourceTests.m @@ -0,0 +1,42 @@ +#import <XCTest/XCTest.h> + +#import <Mapbox/Mapbox.h> + +@interface MGLImageSourceTests : XCTestCase + +@end + +@implementation MGLImageSourceTests + + +- (void)testMGLImageSourceWithImageURL { + + MGLCoordinateQuad quad = { { 80, 37}, { 81, 37}, { 81, 39}, { 80, 39}}; + MGLImageSource *source = [[MGLImageSource alloc] initWithIdentifier:@"source-id" coordinates:quad imageURL: [NSURL URLWithString:@"http://host/image.png"]]; + + XCTAssertNotNil(source.URL); + XCTAssertEqualObjects(source.URL.absoluteString, @"http://host/image.png"); + XCTAssertNil(source.image); +} + +- (void)testMGLImageSourceWithImage { + + NSString *imageName = @"RadarImage"; +#if TARGET_OS_IPHONE + MGLImage *image = [MGLImage imageNamed:imageName + inBundle:[NSBundle bundleForClass:[self class]] + compatibleWithTraitCollection:nil]; +#else + MGLImage *image = [[NSBundle bundleForClass:[self class]] imageForResource:imageName]; +#endif + XCTAssertNotNil(image); + + MGLCoordinateQuad quad = { { 80, 37}, { 81, 37}, { 81, 39}, { 80, 39}}; + MGLImageSource *source = [[MGLImageSource alloc] initWithIdentifier:@"source-id" coordinates:quad image:image]; + + XCTAssertNotNil(source.image); + XCTAssertEqualObjects(source.image, image); + XCTAssertNil(source.URL); +} + +@end diff --git a/platform/darwin/test/MGLStyleTests.mm b/platform/darwin/test/MGLStyleTests.mm index d93483ea6e..7f82dddfac 100644 --- a/platform/darwin/test/MGLStyleTests.mm +++ b/platform/darwin/test/MGLStyleTests.mm @@ -210,9 +210,9 @@ MGLRasterSource *rasterSource = [[MGLRasterSource alloc] initWithIdentifier:@"some-identifier" tileURLTemplates:@[] options:nil]; [self.style addSource:rasterSource]; - // Attempt to remove a shape source with the same identifier as the raster source - MGLShapeSource *shapeSource = [[MGLShapeSource alloc] initWithIdentifier:@"some-identifier" shape:nil options:nil]; - [self.style removeSource:shapeSource]; + // Attempt to remove an image source with the same identifier as the raster source + MGLImageSource *imageSource = [[MGLImageSource alloc] initWithIdentifier:@"some-identifier" coordinates: { }]; + [self.style removeSource:imageSource]; // The raster source should still be added XCTAssertTrue([[self.style sourceWithIdentifier:rasterSource.identifier] isMemberOfClass:[MGLRasterSource class]]); @@ -220,16 +220,16 @@ [self.style removeSource:rasterSource]; // Add the shape source - [self.style addSource:shapeSource]; + [self.style addSource:imageSource]; // Attempt to remove a vector source with the same identifer as the shape source MGLVectorSource *vectorSource = [[MGLVectorSource alloc] initWithIdentifier:@"some-identifier" tileURLTemplates:@[] options:nil]; [self.style removeSource:vectorSource]; - // The shape source should still be added - XCTAssertTrue([[self.style sourceWithIdentifier:shapeSource.identifier] isMemberOfClass:[MGLShapeSource class]]); + // The image source should still be added + XCTAssertTrue([[self.style sourceWithIdentifier:imageSource.identifier] isMemberOfClass:[MGLImageSource class]]); - // Remove the shape source - [self.style removeSource:shapeSource]; + // Remove the image source + [self.style removeSource:imageSource]; // Add the vector source [self.style addSource:vectorSource]; @@ -237,7 +237,7 @@ // Attempt to remove the previously created raster source that has the same identifer as the shape source [self.style removeSource:rasterSource]; // The vector source should still be added - XCTAssertTrue([[self.style sourceWithIdentifier:shapeSource.identifier] isMemberOfClass:[MGLVectorSource class]]); + XCTAssertTrue([[self.style sourceWithIdentifier:imageSource.identifier] isMemberOfClass:[MGLVectorSource class]]); } - (void)testRemovingSourceInUse { diff --git a/platform/darwin/test/Media.xcassets/RadarImage.imageset/Contents.json b/platform/darwin/test/Media.xcassets/RadarImage.imageset/Contents.json new file mode 100644 index 0000000000..79be9ed970 --- /dev/null +++ b/platform/darwin/test/Media.xcassets/RadarImage.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "filename" : "radar.png", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +}
\ No newline at end of file diff --git a/platform/darwin/test/Media.xcassets/RadarImage.imageset/radar.png b/platform/darwin/test/Media.xcassets/RadarImage.imageset/radar.png Binary files differnew file mode 100644 index 0000000000..e23697f42a --- /dev/null +++ b/platform/darwin/test/Media.xcassets/RadarImage.imageset/radar.png |