Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Examples/Examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extern NSString *const MBXExampleHeatmap;
extern NSString *const MBXExampleImageAnnotation;
extern NSString *const MBXExampleImageSource;
extern NSString *const MBXExampleLineAnnotationGeoJSON;
extern NSString *const MBXExampleLineGradient;
extern NSString *const MBXExampleLineStyleLayer;
extern NSString *const MBXExampleLiveData;
extern NSString *const MBXExampleMultipleImages;
Expand Down
1 change: 1 addition & 0 deletions Examples/Examples.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ + (NSArray *)groups {
@"title": @"Add a heatmap layer"
},
@{@"className": MBXExampleImageSource, @"title": @"Add an image"},
@{@"className": MBXExampleLineGradient, @"title": @"Style with a gradient"},
@{@"className": MBXExampleLiveData, @"title": @"Add live data"},
@{@"className": MBXExampleMultipleShapes, @"title": @"Add multiple shapes from a single shape source"},
@{@"className": MBXExampleMultipleImages, @"title": @"Add multiple images"},
Expand Down
29 changes: 29 additions & 0 deletions Examples/Files/line-gradient.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {"lineMetrics": true},
"geometry": {
"coordinates": [
[-77.044211, 38.852924 ],
[-77.045659, 38.860158 ],
[-77.044232, 38.862326 ],
[-77.040879, 38.865454 ],
[-77.039936, 38.867698 ],
[-77.040338, 38.86943 ],
[-77.04264, 38.872528 ],
[-77.03696, 38.878424 ],
[-77.032309, 38.87937 ],
[-77.030056, 38.880945 ],
[-77.027645, 38.881779 ],
[-77.026946, 38.882645 ],
[-77.026942, 38.885502 ],
[-77.028054, 38.887449 ],
[-77.02806, 38.892088 ],
[-77.03364, 38.892108 ],
[-77.033643, 38.899926 ]
],
"type": "LineString"
}
}]
}
9 changes: 9 additions & 0 deletions Examples/ObjectiveC/Headers/LineGradientExample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface LineGradientExample : UIViewController

@end

NS_ASSUME_NONNULL_END
58 changes: 58 additions & 0 deletions Examples/ObjectiveC/LineGradientExample.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#import "LineGradientExample.h"
@import Mapbox;

NSString *const MBXExampleLineGradient = @"LineGradientExample";

@interface LineGradientExample ()<MGLMapViewDelegate>

@end

@implementation LineGradientExample

- (void)viewDidLoad {
[super viewDidLoad];
MGLMapView *mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds styleURL:[MGLStyle lightStyleURL]];

mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

mapView.delegate = self;

// Set the map’s center coordinate and zoom level.
[mapView setCenterCoordinate:CLLocationCoordinate2DMake(38.875, -77.035)
zoomLevel:12
animated:NO];

[self.view addSubview:mapView];
}

- (void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle *)style {

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"line-gradient" ofType:@"geojson"]];
MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"line-source" URL:url options:nil];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the clarification that will happen in mapbox/mapbox-gl-native#13000 should allow you to switch back to using an MGLPolyline like you did in 100c88d.



[style addSource:source];

MGLLineStyleLayer *polylineStyle = [[MGLLineStyleLayer alloc] initWithIdentifier:@"lineStyle" source:source];
polylineStyle.lineWidth = [NSExpression expressionForConstantValue:@14];
polylineStyle.lineCap = [NSExpression expressionForConstantValue:@"round"];

NSDictionary *stops = @{
@0: UIColor.blueColor,
@0.1: UIColor.purpleColor,
@0.3: UIColor.cyanColor,
@0.5: UIColor.greenColor,
@0.7: UIColor.yellowColor,
@1: UIColor.redColor
};

NSExpression *gradientExpression = [NSExpression expressionWithFormat:@"mgl_interpolate:withCurveType:parameters:stops:($lineProgress, 'linear', nil, %@)", stops];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting that we might be seeing this crash in its current state because of mapbox/mapbox-gl-native#12917.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can try using MGL_FUNCTION("line-progress") until mapbox/mapbox-gl-native#12917 is resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting the following error when trying to implement that workaround @captainbarbosa:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse function name 'MGL_Function:' into supported selector (MGL_Function:) '

Am I doing something wrong here:

NSExpression *gradientExpression = [NSExpression expressionWithFormat:@"mgl_interpolate:withCurveType:parameters:stops:(MGL_Function('line-progress'), 'linear', nil, %@)", stops];


polylineStyle.lineColor = gradientExpression;

[style addLayer:polylineStyle];

}

@end

14 changes: 14 additions & 0 deletions Examples/Swift/LineGradientExample.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import UIKit
import Mapbox

@objc(LineGradientExample_Swift)

class LineGradientExample: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

}