Skip to content

Commit

Permalink
Adds getClusterChildren for iOS and Android (#1495)
Browse files Browse the repository at this point in the history
  • Loading branch information
tr3v3r authored Aug 20, 2021
1 parent 7ab300d commit 260852b
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,14 @@ public void getClusterLeaves(String callbackID, int clusterId, int number, int o
AndroidCallbackEvent event = new AndroidCallbackEvent(this, callbackID, payload);
mManager.handleEvent(event);
}

public void getClusterChildren(String callbackID, int clusterId) {
Feature clusterFeature = mSource.querySourceFeatures(Expression.eq(Expression.get("cluster_id"), clusterId)).get(0);
FeatureCollection leaves = mSource.getClusterChildren(clusterFeature);
WritableMap payload = new WritableNativeMap();
payload.putString("data", leaves.toJson());

AndroidCallbackEvent event = new AndroidCallbackEvent(this, callbackID, payload);
mManager.handleEvent(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public Map<String, String> customEvents() {
public static final int METHOD_FEATURES = 103;
public static final int METHOD_GET_CLUSTER_EXPANSION_ZOOM = 104;
public static final int METHOD_GET_CLUSTER_LEAVES = 105;
public static final int METHOD_GET_CLUSTER_CHILDREN = 106;

@Nullable
@Override
Expand All @@ -163,6 +164,7 @@ public Map<String, Integer> getCommandsMap() {
.put("features", METHOD_FEATURES)
.put("getClusterExpansionZoom", METHOD_GET_CLUSTER_EXPANSION_ZOOM)
.put("getClusterLeaves", METHOD_GET_CLUSTER_LEAVES)
.put("getClusterChildren", METHOD_GET_CLUSTER_CHILDREN)
.build();
}

Expand All @@ -186,6 +188,12 @@ public void receiveCommand(RCTMGLShapeSource source, int commandID, @Nullable Re
args.getInt((3))
);
break;
case METHOD_GET_CLUSTER_CHILDREN:
source.getClusterChildren(
args.getString(0),
args.getInt(1)
);
break;
}
}
}
16 changes: 16 additions & 0 deletions docs/ShapeSource.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ const collection = await shapeSource.getClusterLeaves(clusterId, limit, offset);
```


#### getClusterChildren(clusterId)

Returns the FeatureCollection from the cluster (on the next zoom level).

##### arguments
| Name | Type | Required | Description |
| ---- | :--: | :------: | :----------: |
| `clusterId` | `number` | `Yes` | The id of the cluster to expand. |



```javascript
const collection = await shapeSource.getClusterChildren(clusterId);
```


#### onPress(event)


Expand Down
27 changes: 27 additions & 0 deletions docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3643,6 +3643,33 @@
"\nconst collection = await shapeSource.getClusterLeaves(clusterId, limit, offset);\n\n"
]
},
{
"name": "getClusterChildren",
"docblock": "Returns the FeatureCollection from the cluster (on the next zoom level).\n\n@example\nconst collection = await shapeSource.getClusterChildren(clusterId);\n\n@param {number} clusterId - The id of the cluster to expand.\n@return {FeatureCollection}",
"modifiers": [
"async"
],
"params": [
{
"name": "clusterId",
"description": "The id of the cluster to expand.",
"type": {
"name": "number"
},
"optional": false
}
],
"returns": {
"description": null,
"type": {
"name": "FeatureCollection"
}
},
"description": "Returns the FeatureCollection from the cluster (on the next zoom level).",
"examples": [
"\nconst collection = await shapeSource.getClusterChildren(clusterId);\n\n"
]
},
{
"name": "onPress",
"docblock": null,
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ declare namespace MapboxGL {
* @param offset the amount of points to skip (for pagination)
*/
getClusterLeaves: (clusterId: number, limit: number, offset: number ) => object
/**
* Returns the children of a cluster (on the next zoom level).
* @param clusterId the ID of the cluster
*/
getClusterChildren: (clusterId: number) => object
}
class RasterSource extends Component<RasterSourceProps> { }

Expand Down
1 change: 1 addition & 0 deletions ios/RCTMGL/RCTMGLShapeSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- (nonnull NSArray<id <MGLFeature>> *)getClusterLeaves:(nonnull NSNumber *)clusterId
number:(NSUInteger)number
offset:(NSUInteger)offset;
- (nonnull NSArray<id <MGLFeature>> *)getClusterChildren:(nonnull NSNumber *)clusterId;

- (double)getClusterExpansionZoom:(nonnull NSNumber *)clusterId;

Expand Down
11 changes: 11 additions & 0 deletions ios/RCTMGL/RCTMGLShapeSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,15 @@ - (double)getClusterExpansionZoom:(nonnull NSNumber *)clusterId
return [shapeSource leavesOfCluster:cluster offset:offset limit:number];
}

- (nonnull NSArray<id <MGLFeature>> *)getClusterChildren:(nonnull NSNumber *)clusterId
{
MGLShapeSource *shapeSource = (MGLShapeSource *)self.source;

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"cluster_id == %@", clusterId];
NSArray<id<MGLFeature>> *features = [shapeSource featuresMatchingPredicate:predicate];

MGLPointFeatureCluster * cluster = (MGLPointFeatureCluster *)features[0];
return [shapeSource childrenOfCluster:cluster];
}

@end
21 changes: 21 additions & 0 deletions ios/RCTMGL/RCTMGLShapeSourceManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,25 @@ - (UIView*)view
}];
}

RCT_EXPORT_METHOD(getClusterChildren:(nonnull NSNumber*)reactTag
clusterId:(nonnull NSNumber *)clusterId
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *manager, NSDictionary<NSNumber*, UIView*> *viewRegistry) {
RCTMGLShapeSource* shapeSource = (RCTMGLShapeSource *)viewRegistry[reactTag];

NSArray<id<MGLFeature>> *shapes = [sтвhapeSource getClusterChildren: clusterId];

NSMutableArray<NSDictionary*> *features = [[NSMutableArray alloc] initWithCapacity:shapes.count];
for (int i = 0; i < shapes.count; i++) {
[features addObject:shapes[i].geoJSONDictionary];
}

resolve(@{
@"data": @{ @"type": @"FeatureCollection", @"features": features }
});
}];
}

@end
23 changes: 23 additions & 0 deletions javascript/components/ShapeSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,29 @@ class ShapeSource extends NativeBridgeComponent(AbstractSource) {
return res.data;
}

/**
* Returns the FeatureCollection from the cluster (on the next zoom level).
*
* @example
* const collection = await shapeSource.getClusterChildren(clusterId);
*
* @param {number} clusterId - The id of the cluster to expand.
* @return {FeatureCollection}
*/
async getClusterChildren(clusterId) {
const res = await this._runNativeCommand(
'getClusterChildren',
this._nativeRef,
[clusterId],
);

if (isAndroid()) {
return JSON.parse(res.data);
}

return res.data;
}

setNativeProps(props) {
const shallowProps = Object.assign({}, props);

Expand Down

0 comments on commit 260852b

Please sign in to comment.