-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRasterTileSourceExample.m
112 lines (82 loc) · 4.05 KB
/
RasterTileSourceExample.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// RasterTileSourceExample.m
// mapboxqs
//
// Created by Tuyen Vu on 11/03/2023.
//
#import "RasterTileSourceExample.h"
#import "MapboxMaps/MapboxMaps-Swift.h"
#import "MapboxMapObjC/MapboxMapObjC-Swift.h"
NSString* sourceId = @"raster-source";
@interface RasterTileSourceExample ()
@property MapView* mapView;
@end
@implementation RasterTileSourceExample {
TMBCancelable* cancelables;
bool isTileRequestDelayEnabled;
UIButton* button;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Initialize a `MapView` that is centered over the southeastern United States.
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(40, -74.5);
TMBCameraOptions* cameraOptions = [[TMBCameraOptions alloc] initWithCenter:centerCoordinate padding:UIEdgeInsetsZero anchor:CGPointZero zoom:2 bearing:0 pitch:0];
MapInitOptions* mapInitOptions = [MapInitOptionsFactory createWithMapOptions:nil cameraOptions:cameraOptions styleURI:[BuiltInStyles satellite] styleJSON:nil antialiasingSampleCount:1];
self.mapView = [MapViewFactory createWithFrame:self.view.bounds options:mapInitOptions];
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Wait for the map to load its style before adding data.
cancelables = [self.mapView.mapboxMap onMapLoaded:^(id _Nonnull _) {
[self addRasterSource];
// The below line is used for internal testing purposes only.
if ([self respondsToSelector:@selector(finish)]) {
[self finish];
}
}];
[self.view addSubview:self.mapView];
button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"Enable tile request delay" forState: UIControlStateNormal];
button.backgroundColor = UIColor.whiteColor;
button.contentEdgeInsets = UIEdgeInsetsMake(8, 16, 8, 16);
button.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:button];
[NSLayoutConstraint activateConstraints:@[
[button.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
[button.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor]
]];
[button addTarget:self action:@selector(toggleTileRequestDelay) forControlEvents:UIControlEventTouchUpInside];
}
- (void) toggleTileRequestDelay {
isTileRequestDelayEnabled = !isTileRequestDelayEnabled;
TMBMapboxMap* mapboxMap = [self.mapView mapboxMap];
[mapboxMap setSourcePropertyFor:sourceId
property:@"tile-requests-delay"
value:isTileRequestDelayEnabled ? @5000 : @0 completion:^(NSError * _Nullable _) {
}];
[button setTitle: isTileRequestDelayEnabled ? @"Disable tile request delay" : @"Enable tile request delay"
forState: UIControlStateNormal];
}
- (void) addRasterSource {
// This URL points to raster tiles from OpenStreetMap
NSString* sourceUrl = @"https://tile.openstreetmap.org/{z}/{x}/{y}.png";
// Create a `RasterSource` and set the source's `tiles` to the Stamen watercolor raster tiles.
TMBRasterSource* rasterSource = [[TMBRasterSource alloc] initWithId:sourceId];
rasterSource.tiles = @[sourceUrl];
// Specify the tile size for the `RasterSource`.
rasterSource.tileSize = @256;
// Specify that the layer should use the source with the ID `raster-source`. This ID will be
// assigned to the `RasterSource` when it is added to the style.
TMBRasterLayer* rasterLayer = [[TMBRasterLayer alloc] initWithId:@"raster-layer" source: sourceId];
TMBMapboxMap* mapboxMap = [self.mapView mapboxMap];
[mapboxMap addSource:rasterSource dataId:nil completion:^(NSError * _Nullable error) {
if (error) {
NSLog(@"ERR add source: %@", error);
}
}];
[mapboxMap addLayer:rasterLayer layerPosition:nil completion:^(NSError * _Nullable error) {
if (error) {
NSLog(@"ERR add layer: %@", error);
}
}];
}
@end