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

Add observer to detect tileset errors #415

Open
wants to merge 4 commits into
base: main
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
24 changes: 24 additions & 0 deletions Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
64CF97101DF224E400C3C27B /* ThirdPartyVectorStyleExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64CF970F1DF224E400C3C27B /* ThirdPartyVectorStyleExample.swift */; };
64CF97121DF224F600C3C27B /* RasterImageryExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64CF97111DF224F600C3C27B /* RasterImageryExample.swift */; };
64CF97171DF2251500C3C27B /* RasterImageryExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 64CF97161DF2251500C3C27B /* RasterImageryExample.m */; };
8F2D8826252737EA0040D381 /* DetectTilesetErrorExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F2D8825252737EA0040D381 /* DetectTilesetErrorExample.swift */; };
8F2D882C252738660040D381 /* DetectTilesetErrorExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F2D882B252738660040D381 /* DetectTilesetErrorExample.m */; };
960A21611D344F9F00BB348B /* DraggableAnnotationViewExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 960A21601D344F9F00BB348B /* DraggableAnnotationViewExample.m */; };
961962911C581700002D3DAB /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 961962901C581700002D3DAB /* main.m */; };
961962941C581700002D3DAB /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 961962931C581700002D3DAB /* AppDelegate.m */; };
Expand Down Expand Up @@ -301,6 +303,12 @@
64CF97111DF224F600C3C27B /* RasterImageryExample.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RasterImageryExample.swift; sourceTree = "<group>"; };
64CF97161DF2251500C3C27B /* RasterImageryExample.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RasterImageryExample.m; sourceTree = "<group>"; };
64CF97191DF2252C00C3C27B /* RasterImageryExample.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RasterImageryExample.h; sourceTree = "<group>"; };
7556D44879C1E2866B0355B8 /* Pods-ExamplesTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ExamplesTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-ExamplesTests/Pods-ExamplesTests.release.xcconfig"; sourceTree = "<group>"; };
7692D1D792EB3849E8754E6B /* Pods_ExamplesUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ExamplesUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7823ECBC65EEAAC3EA0B6E42 /* Pods-DocsCode.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DocsCode.release.xcconfig"; path = "Pods/Target Support Files/Pods-DocsCode/Pods-DocsCode.release.xcconfig"; sourceTree = "<group>"; };
8F2D8825252737EA0040D381 /* DetectTilesetErrorExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetectTilesetErrorExample.swift; sourceTree = "<group>"; };
8F2D882B252738660040D381 /* DetectTilesetErrorExample.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DetectTilesetErrorExample.m; sourceTree = "<group>"; };
8F2D8831252739000040D381 /* DetectTilesetErrorExample.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DetectTilesetErrorExample.h; sourceTree = "<group>"; };
960A215F1D344F9F00BB348B /* DraggableAnnotationViewExample.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DraggableAnnotationViewExample.h; sourceTree = "<group>"; };
960A21601D344F9F00BB348B /* DraggableAnnotationViewExample.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DraggableAnnotationViewExample.m; sourceTree = "<group>"; };
96115A661CAD4E1C000963B8 /* OfflinePackExample.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OfflinePackExample.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -855,6 +863,7 @@
969E7FDB1D25C31700663F84 /* UserTrackingModesExample.h */,
64BBDAFB1DF24DD700BB705D /* WebAPIDataExample.h */,
583BAE092481B0220079BB57 /* InsetMapExample.h */,
8F2D8831252739000040D381 /* DetectTilesetErrorExample.h */,
C67C17E12507FC0C004112DE /* LocationPrivacyExample.h */,
);
path = Headers;
Expand All @@ -876,6 +885,7 @@
isa = PBXGroup;
children = (
A42F4A4C22EF9A79005097F3 /* CacheManagementExample.m */,
8F2D882B252738660040D381 /* DetectTilesetErrorExample.m */,
);
name = Advanced;
sourceTree = "<group>";
Expand All @@ -884,6 +894,7 @@
isa = PBXGroup;
children = (
A42F4A5022EF9ACD005097F3 /* CacheManagementExample.swift */,
8F2D8825252737EA0040D381 /* DetectTilesetErrorExample.swift */,
);
name = Advanced;
sourceTree = "<group>";
Expand Down Expand Up @@ -1135,10 +1146,21 @@
files = (
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-DocsCode/Pods-DocsCode-frameworks.sh",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/Mapbox.framework",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/Mapbox.framework.dSYM",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/93C58D95-90B9-30C8-8F60-4BDE32FD7E8E.bcsymbolmap",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/BB87D8DD-493F-37AA-BD21-2BC609B8311B.bcsymbolmap",
"${BUILT_PRODUCTS_DIR}/MapboxMobileEvents/MapboxMobileEvents.framework",
"$(TARGET_BUILD_DIR)/$(INFOPLIST_PATH)",
);
name = "Insert Mapbox Access Token";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Mapbox.framework",
"${DWARF_DSYM_FOLDER_PATH}/Mapbox.framework.dSYM",
"${BUILT_PRODUCTS_DIR}/93C58D95-90B9-30C8-8F60-4BDE32FD7E8E.bcsymbolmap",
"${BUILT_PRODUCTS_DIR}/BB87D8DD-493F-37AA-BD21-2BC609B8311B.bcsymbolmap",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxMobileEvents.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down Expand Up @@ -1375,6 +1397,7 @@
646B62971DEF6DAF000AA523 /* ShowHideLayerExample.swift in Sources */,
05FA539B1FE2FA00001F3D7D /* StudioClassicStyleExample.m in Sources */,
3E9EF27C209CFD460053D1D7 /* HeatmapExample.swift in Sources */,
8F2D8826252737EA0040D381 /* DetectTilesetErrorExample.swift in Sources */,
583BAE082481AFF50079BB57 /* InsetMapExample.m in Sources */,
1F07015C225422B90045E061 /* LabelPlacementExample.swift in Sources */,
CA3B162E2098CA7E005C087B /* TestingSupport.m in Sources */,
Expand Down Expand Up @@ -1436,6 +1459,7 @@
968247271C5C1DC700494AB8 /* CameraAnimationExample.m in Sources */,
CA39B2BF209B881300D37037 /* BuildingLightExample+UITesting.m in Sources */,
961962D51C583FDC002D3DAB /* ExamplesContainerViewController.m in Sources */,
8F2D882C252738660040D381 /* DetectTilesetErrorExample.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
1 change: 1 addition & 0 deletions Examples/Examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extern NSString *const MBXExampleCustomCalloutView;
extern NSString *const MBXExampleDDSCircleLayer;
extern NSString *const MBXExampleDefaultCallout;
extern NSString *const MBXExampleDefaultStyles;
extern NSString *const MBXExampleDetectTilesetError;
extern NSString *const MBXExampleDraggableAnnotationView;
extern NSString *const MBXExampleFeatureSelection;
extern NSString *const MBXExampleFormattingExpression;
Expand Down
1 change: 1 addition & 0 deletions Examples/Examples.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ + (NSArray *)groups {
@"title": @"Advanced",
@"examples": @[
@{@"className": MBXExampleCacheManagement, @"title": @"Use cache management methods"},
@{@"className": MBXExampleDetectTilesetError, @"title": @"Detect tileset errors"},
]
},
@{
Expand Down
80 changes: 80 additions & 0 deletions Examples/ObjectiveC/DetectTilesetErrorExample.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#import "DetectTilesetErrorExample.h"
@import Mapbox;

NSString *const MBXExampleDetectTilesetError = @"DetectTilesetErrorExample";


@interface MyObserver: MGLObserver
@end

@implementation MyObserver
- (void) notifyWithEvent:(MGLEvent *)event {
[super notifyWithEvent:event];
NSDictionary* data = (NSDictionary*) event.data;
if (!data) {
return;
}
NSDictionary* request = (NSDictionary*)[data valueForKey: @"request"];
if (!request) {
return;
}
NSString* dataSource = [data valueForKey: @"data-source"];
if (![dataSource isEqual: @"network"]) {
return;
}
NSDictionary* response = (NSDictionary*)[data valueForKey: @"response"];
if (!response) {
return;
}
NSDictionary* error = [response valueForKey: @"error"];
NSLog(@"network request: %@, errors: %@", request, error);
}
@end


@interface DetectTilesetErrorExample () <MGLMapViewDelegate>
@property (nonatomic) MGLMapView *mapView;
@property (nonatomic) MGLStyleLayer *contoursLayer;
@property (nonatomic) MyObserver *observer;
@end

@implementation DetectTilesetErrorExample

- (void)viewDidLoad {
[super viewDidLoad];

self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds];
self.observer = [[MyObserver alloc] init];
[self.mapView subscribeForObserver: self.observer event: MGLEventTypeResourceRequest];
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

// Set the map's center coordinate
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(37.745395, -119.594421)
zoomLevel:11
animated:NO];

[self.view addSubview:self.mapView];

// Set the delegate property of our map view to self after instantiating it
self.mapView.delegate = self;
}

- (void)dealloc {
if (self.observer) {
[self.mapView unsubscribeForObserver: self.observer];
}
}

// Wait until the style is loaded before modifying the map style
- (void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle *)style {
[self addLayer];
}

- (void)addLayer {
MGLSource *source = [[MGLVectorTileSource alloc] initWithIdentifier:@"contours" configurationURL:[NSURL URLWithString:@"mapbox://badtilesource"]];
MGLLineStyleLayer *layer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"contours" source:source];
[self.mapView.style addSource:source];
[self.mapView.style addLayer:layer];
}

@end
14 changes: 14 additions & 0 deletions Examples/ObjectiveC/Headers/DetectTilesetErrorExample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// DetectTilesetErrorExample.h
// Examples
//
// Created by Takuto Suzuki on 2020/10/02.
// Copyright © 2020 Mapbox. All rights reserved.
//


#import <UIKit/UIKit.h>

@interface DetectTilesetErrorExample : UIViewController

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

@objc(DetectTilesetErrorExample_Swift)

class DetectTilesetErrorExample_Swift: UIViewController, MGLMapViewDelegate {
var mapView: MGLMapView!
var contoursLayer: MGLStyleLayer?
var observer: MyObserver?

override func viewDidLoad() {
super.viewDidLoad()

mapView = MGLMapView(frame: view.bounds)
observer = MyObserver()
mapView.subscribe(for: observer!, event: .resourceRequest)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

mapView.setCenter(CLLocationCoordinate2D(latitude: 37.745395, longitude: -119.594421), zoomLevel: 11, animated: false)
view.addSubview(mapView)

mapView.delegate = self
}

/// We strongly recommending unsubscribing from the observer
deinit {
if let observer = observer {
mapView.unsubscribe(for: observer)
}
}

// Wait until the style is loaded before modifying the map style
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
addLayer(to: style)
}

func addLayer(to style: MGLStyle) {
let source = MGLVectorTileSource(identifier: "contours", configurationURL: NSURL(string: "mapbox://badtilesource")! as URL)
let layer = MGLLineStyleLayer(identifier: "contours", source: source)
style.addSource(source)
style.addLayer(layer)
}
}

class MyObserver: MGLObserver {
override func notify(with event: MGLEvent) {
super.notify(with: event)
guard let data = event.data as? [String: Any] else {
return
}
guard let request = data["request"] as? [String: Any] else {
return
}
guard let dataSource = data["data-source"] as? String else {
return
}
if dataSource != "network" {
return
}
guard let response = data["response"] as? [String: Any] else {
return
}
guard let error = response["error"] as? [String: Any] else {
return
}
print("network request: \(request), errors: \(error)")
}
}
3 changes: 2 additions & 1 deletion Examples/Swift/ShowHideLayerExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Mapbox
class ShowHideLayerExample_Swift: UIViewController, MGLMapViewDelegate {
var mapView: MGLMapView!
var contoursLayer: MGLStyleLayer?
var observer: MGLObserver?

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -26,7 +27,7 @@ class ShowHideLayerExample_Swift: UIViewController, MGLMapViewDelegate {
}

func addLayer(to style: MGLStyle) {
let source = MGLVectorTileSource(identifier: "contours", configurationURL: NSURL(string: "mapbox://mapbox.mapbox-terrain-v2")! as URL)
let source = MGLVectorTileSource(identifier: "contours", configurationURL: NSURL(string: "mapbox://mapbox.mapbox-terrain-v2ssss")! as URL)

let layer = MGLLineStyleLayer(identifier: "contours", source: source)
layer.sourceLayerIdentifier = "contour"
Expand Down