summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2016-08-15 23:39:45 -0700
committerMinh Nguyễn <mxn@1ec5.org>2016-08-17 23:17:39 -0700
commit7b1f9011dbcf0fb7b005ceebc5b91a4f292a1796 (patch)
treedee24e71b9f475701cb484513257df45c7405194 /platform
parent61df0b1bfa0cd43bcc9a87f73f7ece6fbb9b4175 (diff)
downloadqtlocation-mapboxgl-7b1f9011dbcf0fb7b005ceebc5b91a4f292a1796.tar.gz
[ios, macos] More MGLGeoJSONSource options
MGLGeoJSONSource can now be initialized with GeoJSON data. Replaced the data property with a geoJSONData property and added features and URL properties alongside it. Each property may be set or unset based on how the object was initialized. Fixes #5965, fixes #5966.
Diffstat (limited to 'platform')
-rw-r--r--platform/darwin/src/MGLGeoJSONSource.h53
-rw-r--r--platform/darwin/src/MGLGeoJSONSource.mm35
2 files changed, 70 insertions, 18 deletions
diff --git a/platform/darwin/src/MGLGeoJSONSource.h b/platform/darwin/src/MGLGeoJSONSource.h
index 58c0f6b794..ca6ccb5b19 100644
--- a/platform/darwin/src/MGLGeoJSONSource.h
+++ b/platform/darwin/src/MGLGeoJSONSource.h
@@ -1,9 +1,60 @@
#import "MGLSource.h"
+#import "MGLTypes.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@protocol MGLFeature;
+
@interface MGLGeoJSONSource : MGLSource
-@property (nonatomic, readonly, copy) NSString *data;
+/**
+ The contents of the source.
+
+ If the receiver was initialized using `-initWithSourceIdentifier:URL:`, this
+ property is set to `nil`. This property is unavailable until the receiver is
+ passed into `-[MGLStyle addSource]`.
+ */
+@property (nonatomic, readonly, nullable) NS_ARRAY_OF(id <MGLFeature>) *features;
+
+/**
+ A GeoJSON representation of the contents of the source.
+
+ Use the `features` property instead to get an object representation of the
+ contents. Alternatively, use NSJSONSerialization with the value of this
+ property to transform it into Foundation types.
+
+ If the receiver was initialized using `-initWithSourceIdentifier:URL:`, this
+ property is set to `nil`. This property is unavailable until the receiver is
+ passed into `-[MGLStyle addSource]`.
+ */
+@property (nonatomic, readonly, nullable, copy) NSData *geoJSONData;
+/**
+ The URL to the GeoJSON document that specifies the contents of the source.
+
+ If the receiver was initialized using `-initWithSourceIdentifier:geoJSONData:`,
+ this property is set to `nil`.
+ */
+@property (nonatomic, readonly, nullable) NSURL *URL;
+
+/**
+ Initializes a source with the given identifier and GeoJSON data.
+
+ @param sourceIdentifier A string that uniquely identifies the source.
+ @param geoJSONData An NSData object representing GeoJSON source code.
+ */
+- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier geoJSONData:(NSData *)data NS_DESIGNATED_INITIALIZER;
+
+/**
+ Initializes a source with the given identifier and URL.
+
+ @param sourceIdentifier A string that uniquely identifies the source.
+ @param URL An HTTP(S) URL, absolute file URL, or local file URL relative to the
+ current application’s resource bundle.
+ */
- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url NS_DESIGNATED_INITIALIZER;
@end
+
+NS_ASSUME_NONNULL_END
diff --git a/platform/darwin/src/MGLGeoJSONSource.mm b/platform/darwin/src/MGLGeoJSONSource.mm
index 202b6409aa..b411e53429 100644
--- a/platform/darwin/src/MGLGeoJSONSource.mm
+++ b/platform/darwin/src/MGLGeoJSONSource.mm
@@ -1,26 +1,24 @@
#import "MGLGeoJSONSource.h"
#import "MGLSource_Private.h"
+#import "MGLFeature_Private.h"
-#include <mbgl/style/sources/geojson_source.hpp>
-
-@interface MGLGeoJSONSource ()
+#import "NSURL+MGLAdditions.h"
-@property (nonatomic, copy) NSURL *URL;
-
-@end
+#include <mbgl/style/sources/geojson_source.hpp>
@implementation MGLGeoJSONSource
-- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url
-{
+- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier geoJSONData:(NSData *)data {
+ if (self = [super initWithSourceIdentifier:sourceIdentifier]) {
+ _geoJSONData = data;
+ }
+ return self;
+}
+
+- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url {
if (self = [super initWithSourceIdentifier:sourceIdentifier]) {
_URL = url;
- if (url.isFileURL) {
- _data = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL];
- } else {
- _data = url.absoluteString;
- }
}
return self;
}
@@ -28,11 +26,14 @@
- (std::unique_ptr<mbgl::style::Source>)mbgl_source
{
auto source = std::make_unique<mbgl::style::GeoJSONSource>(self.sourceIdentifier.UTF8String);
- if (_URL.isFileURL) {
- const auto geojson = mapbox::geojson::parse(self.data.UTF8String).get<mapbox::geojson::feature_collection>();
- source->setGeoJSON(geojson);
+ if (self.URL) {
+ NSURL *url = self.URL.mgl_URLByStandardizingScheme;
+ source->setURL(url.absoluteString.UTF8String);
} else {
- source->setURL(self.data.UTF8String);
+ NSString *string = [[NSString alloc] initWithData:self.geoJSONData encoding:NSUTF8StringEncoding];
+ const auto geojson = mapbox::geojson::parse(string.UTF8String).get<mapbox::geojson::feature_collection>();
+ source->setGeoJSON(geojson);
+ _features = MGLFeaturesFromMBGLFeatures(geojson);
}
return std::move(source);
}