Skip to content

Instantly share code, notes, and snippets.

@boundsj
Last active September 21, 2016 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boundsj/c39973346a27104a3fad1647a37606e6 to your computer and use it in GitHub Desktop.
Save boundsj/c39973346a27104a3fad1647a37606e6 to your computer and use it in GitHub Desktop.
earthquake clusters
    NSURL *url = [NSURL URLWithString:@"https://www.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson"];
    MGLSource *source = [[MGLSource alloc] initWithSourceIdentifier:@"source"];
    MGLSymbolStyleLayer *symbolLayer = [[MGLSymbolStyleLayer alloc] initWithLayerIdentifier:@"place-city-sm" source:source];
    
    NSDictionary *options = @{MGLGeoJSONClusterOption: @(YES),
                              MGLGeoJSONClusterRadiusOption: @10,
                              MGLGeoJSONClusterMaximumZoomLevelOption: @15};
    
    MGLGeoJSONSource *geoJSONSource = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"earthquakes" URL:url options:options];
    [self.mapView.style addSource:geoJSONSource];
    
    NSArray *layers = @[@[@150.0, [UIColor colorWithRed:229/255.0 green:94/255.0 blue:94/255.0 alpha:1]],
                        @[@20.0,  [UIColor colorWithRed:249/255.0 green:136/255.0 blue:108/255.0 alpha:1]],
                        @[@0.0,   [UIColor colorWithRed:251/255.0 green:176/255.0 blue:59/255.0 alpha:1]]];
    
    for (int i=0; i<layers.count; i++) {
        MGLCircleStyleLayer *circles = [[MGLCircleStyleLayer alloc] initWithLayerIdentifier:[NSString stringWithFormat:@"cluster-%@", @(i)] source:geoJSONSource];
        [circles setCircleColor:layers[i][1]];
        [circles setCircleRadius:@70.0];
        [circles setCircleBlur:@1.0];
        
        NSPredicate *gtePredicate = [NSPredicate predicateWithFormat:@"%K >= %@", @"point_count", layers[i][0]];
        NSPredicate *predicate = i == 0 ? gtePredicate : [NSCompoundPredicate andPredicateWithSubpredicates:@[gtePredicate, [NSPredicate predicateWithFormat:@"%K < %@", @"point_count", layers[i-1][0]]]];
        
        [circles setPredicate:predicate];
        [self.mapView.style insertLayer:circles belowLayer:symbolLayer];
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment