Skip to content

Commit

Permalink
add mapViewModifier
Browse files Browse the repository at this point in the history
  • Loading branch information
hactar committed Jan 15, 2024
1 parent 39e1d89 commit 922f306
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion Sources/MapLibreSwiftUI/MapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public struct MapView: UIViewRepresentable {

let styleSource: MapStyleSource
let userLayers: [StyleLayerDefinition]
var mapViewModifier: ((MLNMapView) -> Void)?

public init(
styleURL: URL,
Expand All @@ -28,6 +29,27 @@ public struct MapView: UIViewRepresentable {
) {
self.init(styleURL: styleURL, camera: .constant(initialCamera), makeMapContent)
}

/// Allows you to set properties of the underlying MLNMapView directly.
/// Use this function to modify various properties of the MLNMapView instance.
/// For example, you can enable the display of the user's location on the map by setting `showUserLocation` to true.
///
/// - Parameter modifier: A closure that provides you with an MLNMapView so you can set properties.
/// - Returns: A MapView with the modifications applied.
///
/// Example:
/// ```swift
/// MapView()
/// .mapViewModifier { mapView in
/// mapView.showUserLocation = true
/// }
/// ```
///
public func mapViewModifier(_ modifier: @escaping (MLNMapView) -> Void) -> MapView {
var newMapView = self
newMapView.mapViewModifier = modifier
return newMapView
}

public class Coordinator: NSObject, MLNMapViewDelegate {
var parent: MapView
Expand Down Expand Up @@ -192,11 +214,14 @@ public struct MapView: UIViewRepresentable {

public func updateUIView(_ mapView: MLNMapView, context: Context) {
context.coordinator.parent = self
if let mapViewModifier {
mapViewModifier(mapView)
}
// FIXME: This should be a more selective update
context.coordinator.updateStyleSource(styleSource, mapView: mapView)
context.coordinator.updateLayers(mapView: mapView)

// FIXME: This isn't exactly telling us if the *map* is loaded, and the docs for setCenter say it needs t obe.
// FIXME: This isn't exactly telling us if the *map* is loaded, and the docs for setCenter say it needs to be.
let isStyleLoaded = mapView.style != nil

context.coordinator.updateCamera(mapView: mapView,
Expand Down

0 comments on commit 922f306

Please sign in to comment.