Skip to content

Commit

Permalink
Add NSString initializer MLNVectorTileSource (#3163)
Browse files Browse the repository at this point in the history
  • Loading branch information
louwers authored Jan 21, 2025
1 parent 4ae61fb commit b9b511e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
24 changes: 24 additions & 0 deletions platform/darwin/src/MLNVectorTileSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,30 @@ MLN_EXPORT
- (instancetype)initWithIdentifier:(NSString *)identifier
configurationURL:(NSURL *)configurationURL NS_DESIGNATED_INITIALIZER;

/**
Returns a vector tile source initialized with an identifier and a
string-based configuration URL.
After initializing and configuring the source, add it to a map view’s style
using the ``MLNStyle/addSource:`` method.
The string may be a full HTTP or HTTPS URL or a canonical URL. The string should
point to a JSON file that conforms to the
<a href="https://github.com/mapbox/tilejson-spec/">TileJSON specification</a>.
This constructor can be used for URLs that cause problems with `NSURL`’s URL
parsing behavior. For example, URLs starting with `pmtiles://https://` were
not parsed correctly on iOS 17.
@param identifier A string that uniquely identifies the source in the style to
which it is added.
@param configurationURLString A string to a TileJSON configuration file
describing the source’s contents and other metadata.
@return An initialized vector tile source.
*/
- (instancetype)initWithIdentifier:(NSString *)identifier
configurationURLString:(NSString *)configurationURLString NS_DESIGNATED_INITIALIZER;

/**
Returns a vector tile source initialized an identifier, tile URL templates, and
options.
Expand Down
11 changes: 11 additions & 0 deletions platform/darwin/src/MLNVectorTileSource.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ - (instancetype)initWithIdentifier:(NSString *)identifier configurationURL:(NSUR
return self = [super initWithPendingSource:std::move(source)];
}

- (instancetype)initWithIdentifier:(NSString *)identifier
configurationURLString:(NSString *)configurationURLString
{
auto source = std::make_unique<mbgl::style::VectorSource>(
identifier.UTF8String,
configurationURLString.UTF8String
);

return self = [super initWithPendingSource:std::move(source)];
}

- (instancetype)initWithIdentifier:(NSString *)identifier tileURLTemplates:(NSArray<NSString *> *)tileURLTemplates options:(nullable NSDictionary<MLNTileSourceOption, id> *)options {
mbgl::Tileset tileSet = MLNTileSetFromTileURLTemplates(tileURLTemplates, options);
auto source = std::make_unique<mbgl::style::VectorSource>(identifier.UTF8String, tileSet);
Expand Down
44 changes: 44 additions & 0 deletions platform/ios/app/MBXViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ typedef NS_ENUM(NSInteger, MBXSettingsRuntimeStylingRows) {
#if MLN_DRAWABLE_RENDERER
MBXSettingsRuntimeStylingCustomDrawableLayer,
#endif
MBXSettingsRuntimeStylingAddFoursquarePOIsPMTiles
};

typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
Expand Down Expand Up @@ -460,6 +461,7 @@ - (void)dismissSettings:(__unused id)sender
#if MLN_DRAWABLE_RENDERER
@"Add Custom Drawable Layer",
#endif
@"Add FourSquare POIs PMTiles Layer"
]];
break;
case MBXSettingsMiscellaneous:
Expand Down Expand Up @@ -687,6 +689,9 @@ - (void)performActionForSettingAtIndexPath:(NSIndexPath *)indexPath
[self addCustomDrawableLayer];
break;
#endif
case MBXSettingsRuntimeStylingAddFoursquarePOIsPMTiles:
[self addFoursquarePOIsPMTilesLayer];
break;
default:
NSAssert(NO, @"All runtime styling setting rows should be implemented");
break;
Expand Down Expand Up @@ -1048,6 +1053,45 @@ - (void)addAnnotationWithCustomCallout
[self.mapView showAnnotations:annotations animated:YES];
}

// MARK: - Foursquare PMTiles Implementation
/*
Add the new method that creates the PMTiles vector source and circle layer.
*/
- (void)addFoursquarePOIsPMTilesLayer {
MLNVectorTileSource *foursquareSource = [[MLNVectorTileSource alloc] initWithIdentifier:@"foursquare-10M" configurationURLString:@"pmtiles://https://oliverwipfli.ch/data/foursquare-os-places-10M-2024-11-20.pmtiles"];

// Also works
// MLNVectorTileSource *foursquareSource =
// [[MLNVectorTileSource alloc] initWithIdentifier:@"foursquare-10M"
// tileURLTemplates:@[@"pmtiles://https://oliverwipfli.ch/data/foursquare-os-places-10M-2024-11-20.pmtiles"]
// options:nil];

[self.mapView.style addSource:foursquareSource];

MLNCircleStyleLayer *circleLayer = [[MLNCircleStyleLayer alloc] initWithIdentifier:@"foursquare-10M" source:foursquareSource];
circleLayer.sourceLayerIdentifier = @"place";
circleLayer.maximumZoomLevel = 11;
circleLayer.circleColor = [NSExpression expressionForConstantValue:[UIColor colorWithRed:0.8 green:0.2 blue:0.2 alpha:1.0]];
circleLayer.circleOpacity = [NSExpression expressionWithMLNJSONObject:@[
@"interpolate",
@[@"linear"],
@[@"zoom"],
@6, @0.5,
@11, @0.5,
@14, @1.0
]];
circleLayer.circleRadius = [NSExpression expressionWithMLNJSONObject:@[
@"interpolate",
@[@"linear"],
@[@"zoom"],
@6, @1,
@11, @3,
@16, @4,
@18, @8
]];
[self.mapView.style addLayer:circleLayer];
}

- (void)styleBuildingExtrusions
{
MLNSource* source = [self.mapView.style sourceWithIdentifier:@"composite"];
Expand Down

0 comments on commit b9b511e

Please sign in to comment.