diff options
author | jmkiley <jordan.kiley@mapbox.com> | 2019-10-07 17:36:32 -0700 |
---|---|---|
committer | jmkiley <jordan.kiley@mapbox.com> | 2019-10-07 17:36:32 -0700 |
commit | bedc29bd69e40300ca6be1bc2ffa1f99a2b5c879 (patch) | |
tree | bf3dbc1eb0307dfb08bce0750da6bcd2a51e1101 | |
parent | d63e0e9a7db8fd8ef819134596de9f13ef65bb59 (diff) | |
download | qtlocation-mapboxgl-bedc29bd69e40300ca6be1bc2ffa1f99a2b5c879.tar.gz |
[ios] Address feedback
-rw-r--r-- | platform/darwin/src/MGLShapeSource.h | 22 | ||||
-rw-r--r-- | platform/darwin/src/MGLShapeSource.mm | 17 | ||||
-rw-r--r-- | platform/darwin/test/MGLDocumentationExampleTests.swift | 16 | ||||
-rw-r--r-- | platform/ios/app/MBXViewController.m | 17 |
4 files changed, 34 insertions, 38 deletions
diff --git a/platform/darwin/src/MGLShapeSource.h b/platform/darwin/src/MGLShapeSource.h index f11e45722b..73171dfe17 100644 --- a/platform/darwin/src/MGLShapeSource.h +++ b/platform/darwin/src/MGLShapeSource.h @@ -45,19 +45,27 @@ FOUNDATION_EXTERN MGL_EXPORT const MGLShapeSourceOption MGLShapeSourceOptionClus An `NSDictionary` object where the key is an `NSString`. The dictionary key will be the feature attribute key. The resulting attribute value is aggregated from the clustered points. The dictionary value is an `NSArray` - consisting of two objects. + consisting of two `NSExpression` objects. The first object determines how the attribute values are accumulated from the - cluster points. The resulting value is assigned to the specified attribute key. - You can use one of the following: - - * An `[NSExpression expressionForConstantValue:]` containing a single function string, such as `sum` or `max`, which takes at least two operands. - * An `NSExpression` that takes two expression arguments: `featureAccumulated` and - another valid expression. + cluster points. It is an `NSExpression` with an expression function that accepts + two or more arguments (such as `sum` or `max`). The arguments should be + `featureAccumulated` and the previously defined feature attribute key. The + resulting value is assigned to the specified attribute key. The second `NSExpression` in the array determines which attribute values are accessed from individual features within a cluster. + ```swift + let key = "sumValue" + let firstExpression = NSExpression(format: "sum({ $featureAccumulated, %@ })", key) + let secondExpression = NSExpression(forKeyPath: "magnitude") + let clusterPropertiesDictionary = [key : [firstExpression, secondExpression]] + + let options : [MGLShapeSourceOption : Any] = [.clustered : true, + .clusterProperties: clusterPropertiesDictionary] + ``` + This option corresponds to the <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-geojson-clusterProperties"><code>clusterProperties</code></a> source property in the Mapbox Style Specification. diff --git a/platform/darwin/src/MGLShapeSource.mm b/platform/darwin/src/MGLShapeSource.mm index 8bf0a3d3e5..3820fe9d60 100644 --- a/platform/darwin/src/MGLShapeSource.mm +++ b/platform/darwin/src/MGLShapeSource.mm @@ -107,28 +107,19 @@ mbgl::style::GeoJSONOptions MGLGeoJSONOptionsFromDictionary(NSDictionary<MGLShap format:@"MGLShapeSourceOptionClusterProperties member value requires array of two objects."]; } - // reduceExpression could be either a string of operator, or a valid NSExpression - if (![expressionsArray.firstObject isKindOfClass:[NSExpression class]]) { + // reduceExpression should be a valid NSExpression + NSExpression *reduceExpression = expressionsArray[0]; + if (![reduceExpression isKindOfClass:[NSExpression class]]) { [NSException raise:NSInvalidArgumentException format:@"MGLShapeSourceOptionClusterProperties array value requires two expression objects."]; } - NSExpression *reduceOperator = expressionsArray[0]; - NSExpression *reduceExpression; - if (reduceOperator.expressionType == NSConstantValueExpressionType) { - // If reduceOperator is a string, prepare a full NSExpression before parsing - NSString *reduceString = [NSString stringWithFormat:@"%@({ $featureAccumulated, %@})", reduceOperator.constantValue, key]; - reduceExpression = [NSExpression expressionWithFormat:reduceString]; - } else { - reduceExpression = expressionsArray[0]; - } - auto reduce = MGLClusterPropertyFromNSExpression(reduceExpression); if (!reduce) { [NSException raise:NSInvalidArgumentException format:@"Failed to convert MGLShapeSourceOptionClusterProperties reduce expression."]; } - // mapExpression shall be a valid NSExpression + // mapExpression should be a valid NSExpression NSExpression *mapExpression = expressionsArray[1]; if (![mapExpression isKindOfClass:[NSExpression class]]) { [NSException raise:NSInvalidArgumentException diff --git a/platform/darwin/test/MGLDocumentationExampleTests.swift b/platform/darwin/test/MGLDocumentationExampleTests.swift index 9fbb0cc329..9505b144d9 100644 --- a/platform/darwin/test/MGLDocumentationExampleTests.swift +++ b/platform/darwin/test/MGLDocumentationExampleTests.swift @@ -554,7 +554,21 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate { XCTAssertNotNil(attributedExpression) } - + + func testMGLShapeSourceOptionClusterProperties() { + + + //#-example-code + let key = "sumValue" + let firstExpression = NSExpression(format: "sum({ $featureAccumulated, %@ })", key) + let secondExpression = NSExpression(forKeyPath: "magnitude") + let clusterPropertiesDictionary = [key : [firstExpression, secondExpression]] + + let options : [MGLShapeSourceOption : Any] = [.clustered : true, + .clusterProperties: clusterPropertiesDictionary] + //#-end-example-code + + } // For testMGLMapView(). func myCustomFunction() {} } diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 2b35bd697b..82a68e074a 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -2202,23 +2202,6 @@ CLLocationCoordinate2D randomWorldCoordinate() { // that a device with an English-language locale is already effectively // using locale-based country labels. _localizingLabels = [[self bestLanguageForUser] isEqualToString:@"en"]; - - NSURL *url = [NSURL URLWithString:@"https://www.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson"]; - - NSExpression *reduceExpression1 = [NSExpression expressionForConstantValue:@"sum"]; - - NSExpression *mapExpression1 = [NSExpression expressionForKeyPath:@"mag"]; - NSArray *expressArray1 = @[reduceExpression1, mapExpression1]; - - MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"earthquakes" URL:url options:@{ - MGLShapeSourceOptionClustered: @(YES), - MGLShapeSourceOptionClusterRadius: @(15), - MGLShapeSourceOptionClusterProperties: @{@"sumVal" : expressArray1 }}]; - [mapView.style addSource:source]; - MGLSymbolStyleLayer *clusterLayer = [[MGLSymbolStyleLayer alloc] initWithIdentifier:@"clusteredPortsNumbers" source:source]; - clusterLayer.text = [NSExpression expressionWithFormat:@"CAST(sumVal, 'NSString')"]; - [self.mapView.style addLayer:clusterLayer]; - } - (BOOL)mapView:(MGLMapView *)mapView shouldChangeFromCamera:(MGLMapCamera *)oldCamera toCamera:(MGLMapCamera *)newCamera { |