Skip to content

Commit 605d855

Browse files
authored
Merge pull request #39 from HudHud-Maps/enhancement/unify-navigation
Support Maplibre Navigation iOS
2 parents 50673f0 + aece53f commit 605d855

22 files changed

+178
-69
lines changed

.swiftpm/xcode/xcshareddata/xcschemes/MapLibreSwiftDSL.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1510"
3+
LastUpgradeVersion = "1540"
44
version = "1.7">
55
<BuildAction
66
parallelizeBuildables = "YES"

.swiftpm/xcode/xcshareddata/xcschemes/MapLibreSwiftUI-Package.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1510"
3+
LastUpgradeVersion = "1540"
44
version = "1.7">
55
<BuildAction
66
parallelizeBuildables = "YES"

.swiftpm/xcode/xcshareddata/xcschemes/MapLibreSwiftUI.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1510"
3+
LastUpgradeVersion = "1540"
44
version = "1.7">
55
<BuildAction
66
parallelizeBuildables = "YES"

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ can move fast without breaking anything important.
2525
* Overlays
2626
* Dynamic styling
2727
* Camera control / animation??
28+
* Navigation
2829
2. Prevent most common classes of mistakes that users make with the lower level APIs (ex: adding the same source twice)
2930
3. Deeper SwiftUI integration (ex: SwiftUI callout views)
3031

@@ -46,6 +47,45 @@ Then, for each target add either the DSL (for just the DSL) or both (for the Swi
4647
Check out the (super basic) [previews at the bottom of MapView.swift](Sources/MapLibreSwiftUI/MapView.swift)
4748
or more detailed [Examples](Sources/MapLibreSwiftUI/Examples) to see how it works in practice.
4849

50+
## Navigation
51+
52+
If you need to support navigation add https://github.com/HudHud-Maps/maplibre-navigation-ios.git to your Package.swift and add this code:
53+
54+
```swift
55+
import MapboxCoreNavigation
56+
import MapboxNavigation
57+
58+
extension NavigationViewController: MapViewHostViewController {
59+
public typealias MapType = NavigationMapView
60+
}
61+
62+
63+
@State var route: Route?
64+
@State var navigationInProgress: Bool = false
65+
66+
@ViewBuilder
67+
var mapView: some View {
68+
MapView<NavigationViewController>(makeViewController: NavigationViewController(dayStyleURL: self.styleURL), styleURL: self.styleURL, camera: self.$mapStore.camera) {
69+
70+
}
71+
.unsafeMapViewControllerModifier { navigationViewController in
72+
navigationViewController.delegate = self.mapStore
73+
if let route = self.route, self.navigationInProgress == false {
74+
let locationManager = SimulatedLocationManager(route: route)
75+
navigationViewController.startNavigation(with: route, locationManager: locationManager)
76+
self.navigationInProgress = true
77+
} else if self.route == nil, self.navigationInProgress == true {
78+
navigationViewController.endNavigation()
79+
self.navigationInProgress = false
80+
}
81+
82+
navigationViewController.mapView.showsUserLocation = self.showUserLocation && self.mapStore.streetView == .disabled
83+
}
84+
.cameraModifierDisabled(self.route != nil)
85+
}
86+
```
87+
We choose this approach so MapLibreSwiftUI is not depdending on maplibre-navigation as most users don't need it.
88+
4989
## Developer Quick Start
5090

5191
This project uses [`swiftformat`](https://github.com/nicklockwood/SwiftFormat) to automatically handle basic swift formatting

Sources/MapLibreSwiftUI/Examples/Gestures.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ import SwiftUI
1818
.iconColor(.white)
1919
}
2020
.onTapMapGesture(on: [tappableID], onTapChanged: { _, features in
21-
print("Tapped on \(features.first)")
21+
print("Tapped on \(features.first?.description ?? "<nil>")")
2222
})
2323
.ignoresSafeArea(.all)
2424
}
2525

2626
#Preview("Tappable Countries") {
2727
MapView(styleURL: demoTilesURL)
2828
.onTapMapGesture(on: ["countries-fill"], onTapChanged: { _, features in
29-
print("Tapped on \(features.first)")
29+
print("Tapped on \(features.first?.description ?? "<nil>")")
3030
})
3131
.ignoresSafeArea(.all)
3232
}

Sources/MapLibreSwiftUI/Examples/Layers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c
124124
.predicate(NSPredicate(format: "cluster != YES"))
125125
}
126126
.onTapMapGesture(on: ["simple-circles-non-clusters"], onTapChanged: { _, features in
127-
print("Tapped on \(features.first)")
127+
print("Tapped on \(features.first?.debugDescription ?? "<nil>")")
128128
})
129129
.expandClustersOnTapping(clusteredLayers: [ClusterLayer(
130130
layerIdentifier: "simple-circles-clusters",

Sources/MapLibreSwiftUI/Examples/Other.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ import SwiftUI
2727
SymbolStyleLayer(identifier: "simple-symbols", source: pointSource)
2828
.iconImage(UIImage(systemName: "mappin")!)
2929
}
30-
.unsafeMapViewModifier { mapView in
30+
.unsafeMapViewControllerModifier { viewController in
3131
// Not all properties have modifiers yet. Until they do, you can use this 'escape hatch' to the underlying
3232
// MLNMapView. Be careful: if you modify properties that the DSL controls already, they may be overridden. This
3333
// modifier is a "hack", not a final function.
34-
mapView.logoView.isHidden = false
35-
mapView.compassViewPosition = .topLeft
34+
viewController.mapView.logoView.isHidden = false
35+
viewController.mapView.compassViewPosition = .topLeft
3636
}
3737
}

Sources/MapLibreSwiftUI/Examples/Preview Helpers.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
import CoreLocation
33

44
let switzerland = CLLocationCoordinate2D(latitude: 47.03041, longitude: 8.29470)
5-
let demoTilesURL =
6-
URL(string: "https://demotiles.maplibre.org/style.json")!
5+
public let demoTilesURL = URL(string: "https://demotiles.maplibre.org/style.json")!

Sources/MapLibreSwiftUI/Extensions/MapLibre/MLNMapViewCameraUpdating.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Mockable
55

66
// NOTE: We should eventually mark the entire protocol @MainActor, but Mockable generates some unsafe code at the moment
77
@Mockable
8-
protocol MLNMapViewCameraUpdating: AnyObject {
8+
public protocol MLNMapViewCameraUpdating: AnyObject {
99
@MainActor var userTrackingMode: MLNUserTrackingMode { get set }
1010
@MainActor var minimumPitch: CGFloat { get set }
1111
@MainActor var maximumPitch: CGFloat { get set }

Sources/MapLibreSwiftUI/Extensions/UIKit/UIGestureRecognizing.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Mockable
22
import UIKit
33

44
@Mockable
5-
protocol UIGestureRecognizing: AnyObject {
5+
public protocol UIGestureRecognizing: AnyObject {
66
@MainActor var state: UIGestureRecognizer.State { get }
77
@MainActor func location(in view: UIView?) -> CGPoint
88
@MainActor func location(ofTouch touchIndex: Int, in view: UIView?) -> CGPoint

0 commit comments

Comments
 (0)