Skip to content

Commit

Permalink
Adds getClusterChildren for iOS and Android (rnmapbox#1495)
Browse files Browse the repository at this point in the history
  • Loading branch information
tr3v3r authored and mikalaiulasevich committed Sep 23, 2021
1 parent 9343534 commit f5a6b36
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ios/RCTMGL/RCTMGLShapeSourceManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,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 @@ -291,6 +291,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 f5a6b36

Please sign in to comment.