Skip to content

Commit 5f6cd79

Browse files
authored
Expose viewAnnotationAvoidLayers (#2435)
1 parent 6d9ab6e commit 5f6cd79

File tree

7 files changed

+36
-1
lines changed

7 files changed

+36
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Mapbox welcomes participation and contributions from everyone.
66

77
* Promote `ClipLayer.clipLayerTypes` and `ClipLayer.clipLayerScope` to stable.
88
* Remove experimental `DirectionalLight.shadowQuality`.
9+
* Add experimental `ViewAnnotationManager.viewAnnotationAvoidLayers` for specifying layers that view annotations should avoid. The API currently only supports line layers.
910
* Add support for the `maxOverscaleFactorForParentTiles` property in `CustomRasterSource` and `CustomGeometrySource`, allowing greater control over tile overscaling behavior when rendering custom raster tiles.
1011
* Add support for experimental *-use-theme propert that allow to override the color theme set on the Map. This is experimental and have several limitations - currently expressions are not supported. Color properties in Lights, Rain, Snow are not supported. *-use-theme for layer applied only after zoom level change.
1112
* Update CoreMaps to 11.10.0-rc.1 and Common to 24.10.0-rc.1.

Sources/Examples/All Examples/Annotations/DynamicViewAnnotationExample.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ final class DynamicViewAnnotationExample: UIViewController, ExampleProtocol {
1212
private var routes = [Route]() {
1313
didSet {
1414
oldValue.forEach { $0.remove() }
15+
// prevent eta labels from showing up on overlapped parts of the route
16+
mapView.viewAnnotations.viewAnnotationAvoidLayers = Set(routes.map(\.layerId))
1517
routes.forEach { route in
1618
route.mapView = mapView
1719
route.display()

Sources/MapboxMaps/Annotations/ViewAnnotationManager.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ public final class ViewAnnotationManager {
8282
Array(objectAnnotations.values)
8383
}
8484

85+
/// Specify layers that view annotations should avoid. This applies to ALL view annotations associated to any layer.
86+
/// The API currently only supports line layers.
87+
@_spi(Experimental)
88+
@_documentation(visibility: public)
89+
public var viewAnnotationAvoidLayers: Set<String> {
90+
get { mapboxMap.viewAnnotationAvoidLayers }
91+
set { mapboxMap.viewAnnotationAvoidLayers = newValue }
92+
}
93+
8594
/// The complete list of annotations associated with the receiver.
8695
@available(*, deprecated, renamed: "allAnnotations", message: "Please use allAnnotations instead, or directly access ViewAnnotation itself")
8796
public var annotations: [UIView: ViewAnnotationOptions] {

Sources/MapboxMaps/Foundation/MapboxMap.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import MapboxCoreMaps
55
import Turf
66

77
protocol MapboxMapProtocol: AnyObject {
8+
var viewAnnotationAvoidLayers: Set<String> { get set }
89
var cameraBounds: CameraBounds { get }
910
var cameraState: CameraState { get }
1011
var size: CGSize { get }
@@ -1666,6 +1667,11 @@ extension MapboxMap {
16661667
// MARK: - View Annotations
16671668

16681669
extension MapboxMap {
1670+
var viewAnnotationAvoidLayers: Set<String> {
1671+
get { __map.getViewAnnotationAvoidLayers() }
1672+
set { __map.setViewAnnotationAvoidLayersForLayerIds(newValue) }
1673+
}
1674+
16691675
func setViewAnnotationPositionsUpdateCallback(_ callback: ViewAnnotationPositionsUpdateCallback?) {
16701676
__map.setViewAnnotationPositionsUpdateListenerFor(callback.map {
16711677
ViewAnnotationPositionsUpdateListenerImpl(callback: $0)

Tests/MapboxMapsTests/Annotations/ViewAnnotationManagerTests.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import XCTest
2-
@testable import MapboxMaps
2+
@testable @_spi(Experimental) import MapboxMaps
33

44
final class ViewAnnotationManagerTests: XCTestCase {
55

@@ -437,6 +437,13 @@ final class ViewAnnotationManagerTests: XCTestCase {
437437
XCTAssertTrue(bounds.contains(forArea: innerBounds, wrappedCoordinates: true))
438438
}
439439

440+
func testAvoidLayers() throws {
441+
let layers: Set<String> = ["my-symbol-layer", "my-fill-layer"]
442+
manager.viewAnnotationAvoidLayers = layers
443+
444+
XCTAssertEqual(manager.viewAnnotationAvoidLayers, layers)
445+
}
446+
440447
// MARK: - Helper functions
441448

442449
@available(*, deprecated)

Tests/MapboxMapsTests/Foundation/MapboxMapTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,4 +508,13 @@ final class MapboxMapTests: XCTestCase {
508508
XCTAssertEqual(CGPoint(x: 102, y: 1).fit(to: size), CGPoint(x: -1, y: -1))
509509
XCTAssertEqual(CGPoint(x: 1, y: 101).fit(to: size), CGPoint(x: -1, y: -1))
510510
}
511+
512+
func testViewAnnotationAvoidLayers() {
513+
let layers: Set<String> = ["my-symbol-layer", "my-fill-layer"]
514+
515+
mapboxMap.viewAnnotationAvoidLayers = layers
516+
517+
XCTAssertEqual(mapboxMap.viewAnnotationAvoidLayers, layers)
518+
XCTAssertEqual(mapboxMap.__testingMap.getViewAnnotationAvoidLayers(), layers)
519+
}
511520
}

Tests/MapboxMapsTests/Foundation/Mocks/MockMapboxMap.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import CoreLocation
33
import UIKit
44

55
final class MockMapboxMap: MapboxMapProtocol {
6+
var viewAnnotationAvoidLayers = Set<String>()
67
var options: MapOptions = MapOptions()
78

89
let events = MapEvents(makeGenericSubject: { _ in

0 commit comments

Comments
 (0)