From 78be30be925a6ff4a11e909231e522e2163d2f2a Mon Sep 17 00:00:00 2001 From: Birk Skyum <74932975+birkskyum@users.noreply.github.com> Date: Fri, 13 Feb 2026 14:23:07 +0100 Subject: [PATCH] Fix crashing of iOS test apps and add Heatmap (#4099) --- platform/darwin/core/http_file_source.mm | 1 + platform/ios/app/MBXViewController.mm | 101 ++++++++++++++++++++++- platform/ios/ios.cmake | 2 + 3 files changed, 100 insertions(+), 4 deletions(-) diff --git a/platform/darwin/core/http_file_source.mm b/platform/darwin/core/http_file_source.mm index 41e400da0..95655f469 100644 --- a/platform/darwin/core/http_file_source.mm +++ b/platform/darwin/core/http_file_source.mm @@ -63,6 +63,7 @@ public: shared->cancel(); if (task) { [task cancel]; + task = nil; } } diff --git a/platform/ios/app/MBXViewController.mm b/platform/ios/app/MBXViewController.mm index b0e1031b7..ef4285bce 100644 --- a/platform/ios/app/MBXViewController.mm +++ b/platform/ios/app/MBXViewController.mm @@ -110,7 +110,8 @@ typedef NS_ENUM(NSInteger, MBXSettingsRuntimeStylingRows) { MBXSettingsRuntimeStylingCustomLatLonGrid, MBXSettingsRuntimeStylingLineGradient, MBXSettingsRuntimeStylingCustomDrawableLayer, - MBXSettingsRuntimeStylingAddFoursquarePOIsPMTiles + MBXSettingsRuntimeStylingAddFoursquarePOIsPMTiles, + MBXSettingsRuntimeStylingHeatmapLayer }; typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) { @@ -488,7 +489,8 @@ CLLocationCoordinate2D randomWorldCoordinate(void) { @"Add Custom Lat/Lon Grid", @"Style Route line with gradient", @"Add Custom Drawable Layer", - @"Add FourSquare POIs PMTiles Layer" + @"Add FourSquare POIs PMTiles Layer", + @"Add Earthquake Heatmap" ]]; break; case MBXSettingsMiscellaneous: @@ -724,6 +726,9 @@ CLLocationCoordinate2D randomWorldCoordinate(void) { case MBXSettingsRuntimeStylingAddFoursquarePOIsPMTiles: [self addFoursquarePOIsPMTilesLayer]; break; + case MBXSettingsRuntimeStylingHeatmapLayer: + [self addEarthquakeHeatmap]; + break; default: NSAssert(NO, @"All runtime styling setting rows should be implemented"); break; @@ -1127,6 +1132,92 @@ CLLocationCoordinate2D randomWorldCoordinate(void) { [self.mapView.style addLayer:circleLayer]; } +// Earthquake Heatmap + +- (void)addEarthquakeHeatmap { + NSURL *earthquakesURL = [NSURL URLWithString:@"https://maplibre.org/maplibre-gl-js/docs/assets/earthquakes.geojson"]; + MLNShapeSource *source = [[MLNShapeSource alloc] initWithIdentifier:@"earthquakes" URL:earthquakesURL options:nil]; + [self.mapView.style addSource:source]; + + // Heatmap layer - visible at low zoom, fades out by zoom 9 + MLNHeatmapStyleLayer *heatmapLayer = [[MLNHeatmapStyleLayer alloc] initWithIdentifier:@"earthquakes-heat" source:source]; + heatmapLayer.maximumZoomLevel = 9; + + // Weight by earthquake magnitude + heatmapLayer.heatmapWeight = [NSExpression expressionWithMLNJSONObject:@[ + @"interpolate", @[@"linear"], @[@"get", @"mag"], + @0, @0, + @6, @1 + ]]; + + // Increase intensity with zoom + heatmapLayer.heatmapIntensity = [NSExpression expressionWithMLNJSONObject:@[ + @"interpolate", @[@"linear"], @[@"zoom"], + @0, @1, + @9, @3 + ]]; + + // Color ramp from transparent blue to dark red + heatmapLayer.heatmapColor = [NSExpression expressionWithMLNJSONObject:@[ + @"interpolate", @[@"linear"], @[@"heatmap-density"], + @0, @"rgba(33,102,172,0)", + @0.2, @"rgb(103,169,207)", + @0.4, @"rgb(209,229,240)", + @0.6, @"rgb(253,219,199)", + @0.8, @"rgb(239,138,98)", + @1, @"rgb(178,24,43)" + ]]; + + // Increase radius with zoom + heatmapLayer.heatmapRadius = [NSExpression expressionWithMLNJSONObject:@[ + @"interpolate", @[@"linear"], @[@"zoom"], + @0, @2, + @9, @20 + ]]; + + // Fade out heatmap at high zoom + heatmapLayer.heatmapOpacity = [NSExpression expressionWithMLNJSONObject:@[ + @"interpolate", @[@"linear"], @[@"zoom"], + @7, @1, + @9, @0 + ]]; + + [self.mapView.style addLayer:heatmapLayer]; + + // Circle layer - visible at high zoom, fades in from zoom 7 + MLNCircleStyleLayer *circleLayer = [[MLNCircleStyleLayer alloc] initWithIdentifier:@"earthquakes-circle" source:source]; + + circleLayer.circleRadius = [NSExpression expressionWithMLNJSONObject:@[ + @"interpolate", @[@"linear"], @[@"zoom"], + @7, @[@"interpolate", @[@"linear"], @[@"get", @"mag"], @1, @1, @6, @4], + @16, @[@"interpolate", @[@"linear"], @[@"get", @"mag"], @1, @5, @6, @50] + ]]; + + circleLayer.circleColor = [NSExpression expressionWithMLNJSONObject:@[ + @"interpolate", @[@"linear"], @[@"get", @"mag"], + @1, @"rgba(33,102,172,0)", + @2, @"rgb(103,169,207)", + @3, @"rgb(209,229,240)", + @4, @"rgb(253,219,199)", + @5, @"rgb(239,138,98)", + @6, @"rgb(178,24,43)" + ]]; + + circleLayer.circleOpacity = [NSExpression expressionWithMLNJSONObject:@[ + @"interpolate", @[@"linear"], @[@"zoom"], + @7, @0, + @8, @1 + ]]; + + circleLayer.circleStrokeColor = [NSExpression expressionForConstantValue:[UIColor whiteColor]]; + circleLayer.circleStrokeWidth = [NSExpression expressionForConstantValue:@1]; + + [self.mapView.style insertLayer:circleLayer belowLayer:heatmapLayer]; + + // Move camera to show earthquake data (Central America / Pacific region) + [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(15.0, -94.0) zoomLevel:5 animated:YES]; +} + - (void)styleBuildingExtrusions { MLNSource* source = [self.mapView.style sourceWithIdentifier:@"composite"]; @@ -2342,9 +2433,11 @@ CLLocationCoordinate2D randomWorldCoordinate(void) { /// This is hte same style as above but copied locally and the three instances of the metal plug-in layer added to the style /// Look for "type": "plugin-layer-metal-rendering" in the PluginLayerTestStyle.json for an example of how the layer is defined - [self.styleNames addObject:@"MapLibre Basic - Local With Plugin"]; NSURL *url = [[NSBundle mainBundle] URLForResource:@"PluginLayerTestStyle.json" withExtension:nil]; - [self.styleURLs addObject:url]; + if (url) { + [self.styleNames addObject:@"MapLibre Basic - Local With Plugin"]; + [self.styleURLs addObject:url]; + } /// Add MapLibre Styles if an `apiKey` exists NSString* apiKey = [MLNSettings apiKey]; diff --git a/platform/ios/ios.cmake b/platform/ios/ios.cmake index 94086cad4..1f80ce81c 100644 --- a/platform/ios/ios.cmake +++ b/platform/ios/ios.cmake @@ -10,6 +10,8 @@ target_link_libraries( mbgl-vendor-filesystem ) +set_target_properties(mbgl-core PROPERTIES XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES) + file(GLOB_RECURSE IOS_SDK_SOURCE_FILES "${PROJECT_SOURCE_DIR}/platform/darwin/src/*.m" "${PROJECT_SOURCE_DIR}/platform/darwin/src/*.mm"