Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

adding new example for MGLMatch #370

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
3EC92DB71E78C431001D0503 /* metro-line.geojson in Resources */ = {isa = PBXBuildFile; fileRef = 3EC92DB61E78C431001D0503 /* metro-line.geojson */; };
3ED403411E006B5200230C95 /* CameraFlyToExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3ED403401E006B5200230C95 /* CameraFlyToExample.swift */; };
3ED403481E006BE800230C95 /* CameraFlyToExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 3ED403471E006BE800230C95 /* CameraFlyToExample.m */; };
5825B51E236359CA00EC16D8 /* MGLMatchExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 5825B51D236359CA00EC16D8 /* MGLMatchExample.m */; };
5825B52123635BF300EC16D8 /* MGLMatchExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5825B52023635BF300EC16D8 /* MGLMatchExample.swift */; };
60E0DCA0CEED683C3A4F18F2 /* Pods_Examples.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9EC12D8F1A97963D68BFA871 /* Pods_Examples.framework */; };
646B62971DEF6DAF000AA523 /* ShowHideLayerExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 646B62961DEF6DAF000AA523 /* ShowHideLayerExample.swift */; };
646B629C1DEF6DF1000AA523 /* ShowHideLayerExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 646B629B1DEF6DF1000AA523 /* ShowHideLayerExample.m */; };
Expand Down Expand Up @@ -269,6 +271,9 @@
3ED403461E006BE800230C95 /* CameraFlyToExample.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CameraFlyToExample.h; sourceTree = "<group>"; };
3ED403471E006BE800230C95 /* CameraFlyToExample.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CameraFlyToExample.m; sourceTree = "<group>"; };
532FB53EE0142E4DAC3FB49B /* Pods-Shared-Examples.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Shared-Examples.release.xcconfig"; path = "Pods/Target Support Files/Pods-Shared-Examples/Pods-Shared-Examples.release.xcconfig"; sourceTree = "<group>"; };
5825B51D236359CA00EC16D8 /* MGLMatchExample.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MGLMatchExample.m; sourceTree = "<group>"; };
5825B51F23635A5700EC16D8 /* MGLMatchExample.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLMatchExample.h; sourceTree = "<group>"; };
5825B52023635BF300EC16D8 /* MGLMatchExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MGLMatchExample.swift; sourceTree = "<group>"; };
589ABF8859560819F8B431C7 /* Pods-ExamplesTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ExamplesTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ExamplesTests/Pods-ExamplesTests.debug.xcconfig"; sourceTree = "<group>"; };
646B62961DEF6DAF000AA523 /* ShowHideLayerExample.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShowHideLayerExample.swift; sourceTree = "<group>"; };
646B629B1DEF6DF1000AA523 /* ShowHideLayerExample.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ShowHideLayerExample.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -479,6 +484,7 @@
07C138C11E6F65D000D6F678 /* MultipleShapesExample.swift */,
64CF97111DF224F600C3C27B /* RasterImageryExample.swift */,
1F0701542252D2090045E061 /* TextFormattingExample.swift */,
5825B52023635BF300EC16D8 /* MGLMatchExample.swift */,
);
name = "Dynamic styling";
sourceTree = "<group>";
Expand Down Expand Up @@ -555,6 +561,7 @@
646B62A51DEF7121000AA523 /* AnimatedLineExample.m */,
05FA539F1FE2FA1E001F3D7D /* LineStyleLayerExample.m */,
05FA539D1FE2FA1D001F3D7D /* PolygonPatternExample.m */,
5825B51D236359CA00EC16D8 /* MGLMatchExample.m */,
);
name = "Lines and polygons";
sourceTree = "<group>";
Expand Down Expand Up @@ -810,6 +817,7 @@
3E18060E1EAA800A004DB131 /* UserLocationAnnotationExample.h */,
969E7FDB1D25C31700663F84 /* UserTrackingModesExample.h */,
64BBDAFB1DF24DD700BB705D /* WebAPIDataExample.h */,
5825B51F23635A5700EC16D8 /* MGLMatchExample.h */,
);
path = Headers;
sourceTree = "<group>";
Expand Down Expand Up @@ -1317,6 +1325,7 @@
1F1F84771E53A3B700332CC3 /* BlockingGesturesDelegateExample.swift in Sources */,
05536CB2200F9B0900657097 /* SwitchStylesExample.m in Sources */,
CA39B2C0209B881300D37037 /* AnimatedLineExample+UITesting.m in Sources */,
5825B52123635BF300EC16D8 /* MGLMatchExample.swift in Sources */,
3ED403411E006B5200230C95 /* CameraFlyToExample.swift in Sources */,
9691AAA91C5AAD8F006A58C6 /* SimpleMapViewExample.m in Sources */,
960A21611D344F9F00BB348B /* DraggableAnnotationViewExample.m in Sources */,
Expand Down Expand Up @@ -1356,6 +1365,7 @@
646B62B01DEF7161000AA523 /* AnimatedLineExample.swift in Sources */,
3E52ACD31EE8D8AF0056242C /* LiveDataExample.m in Sources */,
3EA5AA041FAD2EC7007073C0 /* StaticSnapshotExample.swift in Sources */,
5825B51E236359CA00EC16D8 /* MGLMatchExample.m in Sources */,
05FA53A91FE2FB46001F3D7D /* CustomCalloutView.m in Sources */,
05FA53A31FE2FA1E001F3D7D /* LineStyleLayerExample.m in Sources */,
3E44BA55203B54120072DE4B /* MultipleImagesExample.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Examples/Examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extern NSString *const MBXExampleImageSource;
extern NSString *const MBXExampleLabelPlacement;
extern NSString *const MBXExampleLineStyleLayer;
extern NSString *const MBXExampleLiveData;
extern NSString *const MBXExampleMGLMatch;
extern NSString *const MBXExampleMultipleImages;
extern NSString *const MBXExampleMultipleShapes;
extern NSString *const MBXExampleOfflinePack;
Expand Down
1 change: 1 addition & 0 deletions Examples/Examples.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ + (NSArray *)groups {
@{@"className": MBXExampleMultipleShapes, @"title": @"Add multiple shapes from a single shape source"},
@{@"className": MBXExampleMultipleImages, @"title": @"Add multiple images"},
@{@"className": MBXExampleRasterImagery, @"title": @"Add raster imagery"},
@{@"className": MBXExampleMGLMatch, @"title":@"Use expressions to style data"}
]
},
@{
Expand Down
6 changes: 6 additions & 0 deletions Examples/ObjectiveC/Headers/MGLMatchExample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#import <UIKit/UIKit.h>

@interface MGLMatchExample : UIViewController


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

NSString *const MBXExampleMGLMatch = @"MGLMatchExample";

@interface MGLMatchExample () <MGLMapViewDelegate>

@property (nonatomic) MGLMapView *mapView;
//@property (nonatomic) MGLSymbolStyleLayer *symbolLayer;
//@property (nonatomic) MGLStyle *style;


@end


@implementation MGLMatchExample

- (void)viewDidLoad {
[super viewDidLoad];

self.mapView = [[MGLMapView alloc] initWithFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mapView.styleURL = [NSURL URLWithString:@"mapbox://styles/zizim/ck12h05if04ha1co4qr1ra9l2"];
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(36.851, -119.448)
zoomLevel:5
animated:NO];
_mapView.delegate = self;
[self.view addSubview:self.mapView];

}

- (void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle *)style {
// Add our GroJSON source to the map.
NSURL *URL = [NSURL URLWithString:@"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson"];
MGLSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"earthquakes" URL:URL options: nil];
[self.mapView.style addSource:source];

//set default values for color, opacity and radius
UIColor *const defaultColor = UIColor.blueColor;
NSExpression *const defaultRadius = [NSExpression expressionForConstantValue: @3];
NSExpression *const defaultOpacity = [NSExpression expressionForConstantValue: @0.5];
MGLCircleStyleLayer *layer = [[MGLCircleStyleLayer alloc] initWithIdentifier:@"earthquakes" source:source];
// Style the circle layer color, opacity and radius based on type.
layer.circleColor = [NSExpression expressionWithFormat: @"MGL_MATCH(type, 'earthquake', %@, 'explosion', %@, 'quarry blast', %@, %@)", UIColor.magentaColor, UIColor.systemPinkColor, UIColor.systemPurpleColor, defaultColor];
layer.circleOpacity = [NSExpression expressionWithFormat: @"MGL_MATCH(type, 'earthquake', %@, 'explosion', %@, 'quarry blast', %@, %@)", [NSExpression expressionForConstantValue: @0.3], [NSExpression expressionForConstantValue: @0.3], [NSExpression expressionForConstantValue: @0.2], defaultOpacity];
layer.circleRadius = [NSExpression expressionWithFormat:@"MGL_MATCH(type, 'earthquake', %@, 'explosion', %@, 'quarry blast', %@, %@)",
[NSExpression expressionForConstantValue: @3], [NSExpression expressionForConstantValue: @6], [NSExpression expressionForConstantValue: @9], defaultRadius];
layer.circleStrokeColor = [NSExpression expressionForConstantValue: UIColor.darkGrayColor];
layer.circleStrokeWidth = [NSExpression expressionForConstantValue: @1];
[self.mapView.style addLayer:layer];
}

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

@objc(MGLMatchExample_Swift)

class MGLMatchExample_Swift: UIViewController, MGLMapViewDelegate {
var source: MGLShapeSource!

override func viewDidLoad() {
super.viewDidLoad()
let mapView = MGLMapView(frame: view.bounds)
mapView.styleURL = MGLStyle.lightStyleURL
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.tintColor = .gray
mapView.centerCoordinate = CLLocationCoordinate2D(latitude: 36.851, longitude: -119.448)
mapView.zoomLevel = 5
// Set the map view‘s delegate property.
mapView.delegate = self
view.addSubview(mapView)
}

func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
// Add our GroJSON source to the map.
if let url = URL(string: "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson") {
let source = MGLShapeSource(identifier: "earthquakes", url: url, options: nil)
style.addSource(source)
//set default values for color, opacity and radius
let defaultColor = UIColor.blue
let defaultRadius = NSExpression(forConstantValue: 3)
let defaultOpacity = NSExpression(forConstantValue: 0.5)
let layer = MGLCircleStyleLayer(identifier: "earthquakes", source: source)
// Style the circle layer color, opacity and radius based on type.
layer.circleColor = NSExpression(format: "MGL_MATCH(type, 'earthquake', %@, 'explosion', %@, 'quarry blast', %@, %@)", UIColor.magenta, UIColor.systemPink, UIColor.systemPurple, defaultColor)
layer.circleOpacity = NSExpression(format: "MGL_MATCH(type, 'earthquake', %@, 'explosion', %@, 'quarry blast', %@, %@)",NSExpression(forConstantValue: 0.3), NSExpression(forConstantValue: 0.3), NSExpression(forConstantValue: 0.2), defaultOpacity)
layer.circleRadius = NSExpression(format: "MGL_MATCH(type, 'earthquake', %@, 'explosion', %@, 'quarry blast', %@, %@)",
NSExpression(forConstantValue: 3), NSExpression(forConstantValue: 6), NSExpression(forConstantValue: 9), defaultRadius)
layer.circleStrokeColor = NSExpression(forConstantValue: UIColor.darkGray)
layer.circleStrokeWidth = NSExpression(forConstantValue: 1)
style.addLayer(layer)
}
}
}