Skip to content

Commit 1c6be45

Browse files
github-actions[bot]Anka
andauthored
RD-1257: Add Center Clamped to Ground and Center Elevation Getters - Swift SDK (#169)
Co-authored-by: Anka <runner@sjc22-bt143-e517dc39-17f0-454c-9c76-5474b0322da4-268796FFA000.local>
1 parent 6ad1e21 commit 1c6be45

File tree

7 files changed

+142
-3
lines changed

7 files changed

+142
-3
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Copyright (c) 2025, MapTiler
3+
// All rights reserved.
4+
// SPDX-License-Identifier: BSD 3-Clause
5+
//
6+
// GetCenterClampedToGround.swift
7+
// MapTilerSDK
8+
//
9+
10+
package struct GetCenterClampedToGround: MTCommand {
11+
package func toJS() -> JSString {
12+
return "\(MTBridge.mapObject).getCenterClampedToGround();"
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Copyright (c) 2025, MapTiler
3+
// All rights reserved.
4+
// SPDX-License-Identifier: BSD 3-Clause
5+
//
6+
// GetCenterElevation.swift
7+
// MapTilerSDK
8+
//
9+
10+
package struct GetCenterElevation: MTCommand {
11+
package func toJS() -> JSString {
12+
return "\(MTBridge.mapObject).getCenterElevation();"
13+
}
14+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Copyright (c) 2025, MapTiler
3+
// All rights reserved.
4+
// SPDX-License-Identifier: BSD 3-Clause
5+
//
6+
// MTBridgeReturnType+BoolValue.swift
7+
// MapTilerSDK
8+
//
9+
10+
import Foundation
11+
12+
package extension MTBridgeReturnType {
13+
var boolValue: Bool? {
14+
switch self {
15+
case .bool(let value):
16+
return value
17+
case .double(let value):
18+
return value != 0
19+
case .string(let value):
20+
let normalizedValue = value.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
21+
22+
if normalizedValue == "true" {
23+
return true
24+
}
25+
26+
if normalizedValue == "false" {
27+
return false
28+
}
29+
30+
guard let doubleValue = Double(normalizedValue) else {
31+
return nil
32+
}
33+
34+
return doubleValue != 0
35+
default:
36+
return nil
37+
}
38+
}
39+
}

Sources/MapTilerSDK/Map/Extensions/MTNavigable/MTMapView+MTNavigable.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,20 @@ extension MTMapView: MTNavigable {
342342
runCommandWithCoordinateReturnValue(GetCenter(), completion: completionHandler)
343343
}
344344

345+
/// Returns whether the map's center is clamped to the ground.
346+
/// - Parameter completionHandler: A handler block to execute when function finishes.
347+
@available(iOS, deprecated: 16.0, message: "Prefer the async version for modern concurrency handling")
348+
public func getCenterClampedToGround(completionHandler: @escaping (Result<Bool, MTError>) -> Void) {
349+
runCommandWithBoolReturnValue(GetCenterClampedToGround(), completion: completionHandler)
350+
}
351+
352+
/// Returns the elevation of the map's center point, in meters above sea level.
353+
/// - Parameter completionHandler: A handler block to execute when function finishes.
354+
@available(iOS, deprecated: 16.0, message: "Prefer the async version for modern concurrency handling")
355+
public func getCenterElevation(completionHandler: @escaping (Result<Double, MTError>) -> Void) {
356+
runCommandWithDoubleReturnValue(GetCenterElevation(), completion: completionHandler)
357+
}
358+
345359
/// Project coordinates to point on the container.
346360
/// - Parameters:
347361
/// - coordinates: The location to project.
@@ -648,6 +662,34 @@ extension MTMapView {
648662
}
649663
}
650664

665+
/// Returns whether the map's center point is clamped to the ground.
666+
public func getCenterClampedToGround() async -> Bool {
667+
await withCheckedContinuation { continuation in
668+
getCenterClampedToGround { result in
669+
switch result {
670+
case .success(let result):
671+
continuation.resume(returning: result)
672+
case .failure:
673+
continuation.resume(returning: false)
674+
}
675+
}
676+
}
677+
}
678+
679+
/// Returns the elevation of the map's center point, in meters above sea level.
680+
public func getCenterElevation() async -> Double {
681+
await withCheckedContinuation { continuation in
682+
getCenterElevation { result in
683+
switch result {
684+
case .success(let result):
685+
continuation.resume(returning: result)
686+
case .failure:
687+
continuation.resume(returning: .nan)
688+
}
689+
}
690+
}
691+
}
692+
651693
/// Project coordinates to point on the container.
652694
/// - Parameters:
653695
/// - coordinates: The location to project.

Sources/MapTilerSDK/Map/Extensions/MTNavigable/MTNavigable.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ public protocol MTNavigable {
113113
/// Returns the map's current center.
114114
func getCenter() async -> CLLocationCoordinate2D
115115

116+
/// Returns the state of the center clamped to ground flag.
117+
func getCenterClampedToGround() async -> Bool
118+
119+
/// Returns the elevation of the map's center point in meters above sea level.
120+
func getCenterElevation() async -> Double
121+
116122
/// Returns the map's current bearing.
117123
func getBearing() async -> Double
118124

Sources/MapTilerSDK/Map/MTMapView.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,8 @@ extension MTMapView {
374374
do {
375375
let value = try await bridge.execute(command)
376376

377-
if case .bool(let commandValue) = value {
377+
if let commandValue = value.boolValue {
378378
completion?(.success(commandValue))
379-
} else if case .double(let commandValue) = value {
380-
completion?(.success(commandValue != 0))
381379
} else {
382380
MTLogger.log("\(command) returned invalid type.", type: .error)
383381
completion?(.failure(MTError.unsupportedReturnType(description: "Expected bool, got unknown.")))

Tests/MapTilerSDKTests/MTNavigationTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,32 @@ struct MTNavigationTests {
7474
#expect(GetPitch().toJS() == getPitchJS)
7575
}
7676

77+
@Test func getCenterClampedToGroundCommand_shouldMatchJS() async throws {
78+
let getCenterClampedToGroundJS = "\(MTBridge.mapObject).getCenterClampedToGround();"
79+
80+
#expect(GetCenterClampedToGround().toJS() == getCenterClampedToGroundJS)
81+
}
82+
83+
@Test func getCenterElevationCommand_shouldMatchJS() async throws {
84+
let getCenterElevationJS = "\(MTBridge.mapObject).getCenterElevation();"
85+
86+
#expect(GetCenterElevation().toJS() == getCenterElevationJS)
87+
}
88+
89+
@Test func boolValueParsingCoversStringAndNumericValues() async throws {
90+
let trueString = try MTBridgeReturnType(from: "true")
91+
let falseString = try MTBridgeReturnType(from: "false")
92+
let oneString = try MTBridgeReturnType(from: "1")
93+
let zeroString = try MTBridgeReturnType(from: "0")
94+
let unexpectedString = try MTBridgeReturnType(from: "maybe")
95+
96+
#expect(trueString.boolValue == true)
97+
#expect(falseString.boolValue == false)
98+
#expect(oneString.boolValue == true)
99+
#expect(zeroString.boolValue == false)
100+
#expect(unexpectedString.boolValue == nil)
101+
}
102+
77103
@Test func jumpToCommand_shouldMatchJS() async throws {
78104
let cameraOptions = MTCameraOptions(zoom: zoom, bearing: bearing, pitch: pitch)
79105

0 commit comments

Comments
 (0)