|
| 1 | +# Observe Low-Level Events |
| 2 | + |
| 3 | +Learn about the ``MLNMapViewDelegate`` methods for observing map events. |
| 4 | + |
| 5 | +> Warning: These methods are not thread-safe. |
| 6 | +
|
| 7 | +You can observe certain low-level events as they happen. Use these methods to collect metrics or investigate issues during map rendering. This feature is intended primarily for power users. We are always interested in improving observability, so if you have a special use case, feel free to [open an issue or pull request](https://github.com/maplibre/maplibre-native) to extend the types of observability methods. |
| 8 | + |
| 9 | +## Shader Events |
| 10 | + |
| 11 | +Observe shader compilation with ``MLNMapViewDelegate/mapView:shaderWillCompile:backend:defines:`` and ``MLNMapViewDelegate/mapView:shaderDidCompile:backend:defines:``. |
| 12 | + |
| 13 | +<!-- include-example(ObserverExampleShaders) --> |
| 14 | + |
| 15 | +```swift |
| 16 | +func mapView(_: MLNMapView, shaderWillCompile id: Int, backend: Int, defines: String) { |
| 17 | + print("A new shader is being compiled - shaderID:\(id), backend type:\(backend), program configuration:\(defines)") |
| 18 | + } |
| 19 | + |
| 20 | + func mapView(_: MLNMapView, shaderDidCompile id: Int, backend: Int, defines: String) { |
| 21 | + print("A shader has been compiled - shaderID:\(id), backend type:\(backend), program configuration:\(defines)") |
| 22 | + } |
| 23 | +``` |
| 24 | + |
| 25 | +See also: ``MLNMapViewDelegate/mapView:shaderDidFailCompile:backend:defines:``. |
| 26 | + |
| 27 | +## Glyph Loading |
| 28 | + |
| 29 | +Observe glyph loading events with ``MLNMapViewDelegate/mapView:glyphsWillLoad:range:`` and ``MLNMapViewDelegate/mapView:glyphsDidLoad:range:``. |
| 30 | + |
| 31 | +<!-- include-example(ObserverExampleGlyphs) --> |
| 32 | + |
| 33 | +```swift |
| 34 | +func mapView(_: MLNMapView, glyphsWillLoad fontStack: [String], range: NSRange) { |
| 35 | + print("Glyphs are being requested for the font stack \(fontStack), ranging from \(range.location) to \(range.location + range.length)") |
| 36 | + } |
| 37 | + |
| 38 | + func mapView(_: MLNMapView, glyphsDidLoad fontStack: [String], range: NSRange) { |
| 39 | + print("Glyphs have been loaded for the font stack \(fontStack), ranging from \(range.location) to \(range.location + range.length)") |
| 40 | + } |
| 41 | +``` |
| 42 | + |
| 43 | +See also: ``MLNMapViewDelegate/mapView:glyphsDidError:range:``. |
| 44 | + |
| 45 | +## Tile Events |
| 46 | + |
| 47 | +Monitor tile-related actions using the delegate method ``MLNMapViewDelegate/mapView:tileDidTriggerAction:x:y:z:wrap:overscaledZ:sourceID:`` with the ``MLNTileOperation`` type. |
| 48 | + |
| 49 | +<!-- include-example(ObserverExampleTiles) --> |
| 50 | + |
| 51 | +```swift |
| 52 | +func mapView(_: MLNMapView, tileDidTriggerAction operation: MLNTileOperation, |
| 53 | + x: Int, |
| 54 | + y: Int, |
| 55 | + z: Int, |
| 56 | + wrap: Int, |
| 57 | + overscaledZ: Int, |
| 58 | + sourceID: String) |
| 59 | + { |
| 60 | + let tileStr = String(format: "(x: %ld, y: %ld, z: %ld, wrap: %ld, overscaledZ: %ld, sourceID: %@)", |
| 61 | + x, y, z, wrap, overscaledZ, sourceID) |
| 62 | + |
| 63 | + switch operation { |
| 64 | + case MLNTileOperation.requestedFromCache: |
| 65 | + print("Requesting tile \(tileStr) from cache") |
| 66 | + |
| 67 | + case MLNTileOperation.requestedFromNetwork: |
| 68 | + print("Requesting tile \(tileStr) from network") |
| 69 | + |
| 70 | + case MLNTileOperation.loadFromCache: |
| 71 | + print("Loading tile \(tileStr), requested from the cache") |
| 72 | + |
| 73 | + case MLNTileOperation.loadFromNetwork: |
| 74 | + print("Loading tile \(tileStr), requested from the network") |
| 75 | + |
| 76 | + case MLNTileOperation.startParse: |
| 77 | + print("Parsing tile \(tileStr)") |
| 78 | + |
| 79 | + case MLNTileOperation.endParse: |
| 80 | + print("Completed parsing tile \(tileStr)") |
| 81 | + |
| 82 | + case MLNTileOperation.error: |
| 83 | + print("An error occured during proccessing for tile \(tileStr)") |
| 84 | + |
| 85 | + case MLNTileOperation.cancelled: |
| 86 | + print("Pending work on tile \(tileStr)") |
| 87 | + |
| 88 | + case MLNTileOperation.nullOp: |
| 89 | + print("An unknown tile operation was emitted for tile \(tileStr)") |
| 90 | + |
| 91 | + @unknown default: |
| 92 | + assertionFailure() |
| 93 | + } |
| 94 | + } |
| 95 | +``` |
| 96 | + |
| 97 | +## Sprite Loading |
| 98 | + |
| 99 | +Observe sprite loading events with ``MLNMapViewDelegate/mapView:spriteWillLoad:url:`` and ``MLNMapViewDelegate/mapView:spriteDidLoad:url:``. |
| 100 | + |
| 101 | +<!-- include-example(ObserverExampleSprites) --> |
| 102 | + |
| 103 | +```swift |
| 104 | +func mapView(_: MLNMapView, spriteWillLoad id: String, url: String) { |
| 105 | + print("The sprite \(id) has been requested from \(url)") |
| 106 | + } |
| 107 | + |
| 108 | + func mapView(_: MLNMapView, spriteDidLoad id: String, url: String) { |
| 109 | + print("The sprite \(id) has been loaded from \(url)") |
| 110 | + } |
| 111 | +``` |
| 112 | + |
| 113 | +See also: ``MLNMapViewDelegate/mapView:spriteDidError:url:``. |
0 commit comments