Skip to content

Commit 4040a2f

Browse files
authored
Add support for FillStyleLayer (maplibre#66)
* Add support for FillStyleLayer. * linting
1 parent 4f40a6d commit 4040a2f

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
### Added
9+
10+
- Added FillStyleLayer and example.
11+
812
## Version 0.4.2 - 2024-12-03
913

1014
### Fixed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import Foundation
2+
import InternalUtils
3+
import MapLibre
4+
import MapLibreSwiftMacros
5+
6+
// TODO: Other properties and their modifiers
7+
@MLNStyleProperty<UIColor>("fillColor", supportsInterpolation: true)
8+
@MLNStyleProperty<UIColor>("fillOutlineColor", supportsInterpolation: true)
9+
@MLNStyleProperty<Float>("fillOpacity", supportsInterpolation: true)
10+
public struct FillStyleLayer: SourceBoundVectorStyleLayerDefinition {
11+
public let identifier: String
12+
public let sourceLayerIdentifier: String?
13+
public var insertionPosition: LayerInsertionPosition = .aboveOthers
14+
public var isVisible: Bool = true
15+
public var maximumZoomLevel: Float? = nil
16+
public var minimumZoomLevel: Float? = nil
17+
18+
public var source: StyleLayerSource
19+
public var predicate: NSPredicate?
20+
21+
public init(identifier: String, source: Source) {
22+
self.identifier = identifier
23+
self.source = .source(source)
24+
sourceLayerIdentifier = nil
25+
}
26+
27+
public init(identifier: String, source: MLNSource, sourceLayerIdentifier: String? = nil) {
28+
self.identifier = identifier
29+
self.source = .mglSource(source)
30+
self.sourceLayerIdentifier = sourceLayerIdentifier
31+
}
32+
33+
public func makeStyleLayer(style: MLNStyle) -> StyleLayer {
34+
let styleSource = addSource(to: style)
35+
36+
return FillStyleLayerInternal(definition: self, mglSource: styleSource)
37+
}
38+
}
39+
40+
private struct FillStyleLayerInternal: StyleLayer {
41+
private var definition: FillStyleLayer
42+
private let mglSource: MLNSource
43+
44+
public var identifier: String { definition.identifier }
45+
public var insertionPosition: LayerInsertionPosition {
46+
get { definition.insertionPosition }
47+
set { definition.insertionPosition = newValue }
48+
}
49+
50+
public var isVisible: Bool {
51+
get { definition.isVisible }
52+
set { definition.isVisible = newValue }
53+
}
54+
55+
public var maximumZoomLevel: Float? {
56+
get { definition.maximumZoomLevel }
57+
set { definition.maximumZoomLevel = newValue }
58+
}
59+
60+
public var minimumZoomLevel: Float? {
61+
get { definition.minimumZoomLevel }
62+
set { definition.minimumZoomLevel = newValue }
63+
}
64+
65+
init(definition: FillStyleLayer, mglSource: MLNSource) {
66+
self.definition = definition
67+
self.mglSource = mglSource
68+
}
69+
70+
public func makeMLNStyleLayer() -> MLNStyleLayer {
71+
let result = MLNFillStyleLayer(identifier: identifier, source: mglSource)
72+
73+
result.fillColor = definition.fillColor
74+
result.fillOutlineColor = definition.fillOutlineColor
75+
result.fillOpacity = definition.fillOpacity
76+
77+
result.predicate = definition.predicate
78+
79+
return result
80+
}
81+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import MapLibre
2+
import MapLibreSwiftDSL
3+
import SwiftUI
4+
5+
@available(iOS 17.0, *)
6+
#Preview {
7+
@Previewable let source = ShapeSource(identifier: "fillSource") {
8+
MLNPolygonFeature(coordinates: austriaPolygon, count: UInt(austriaPolygon.count))
9+
}
10+
MapView(styleURL: demoTilesURL, camera: .constant(.center(austriaPolygon.first!, zoom: 4))) {
11+
FillStyleLayer(identifier: "fillLayer", source: source)
12+
.fillColor(.red)
13+
.fillOpacity(0.5)
14+
.fillOutlineColor(.blue)
15+
}
16+
17+
.ignoresSafeArea(.all)
18+
}

Sources/MapLibreSwiftUI/Examples/Preview Helpers.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,14 @@ import CoreLocation
33

44
let switzerland = CLLocationCoordinate2D(latitude: 47.03041, longitude: 8.29470)
55
public let demoTilesURL = URL(string: "https://demotiles.maplibre.org/style.json")!
6+
7+
let austriaPolygon: [CLLocationCoordinate2D] = [
8+
CLLocationCoordinate2D(latitude: 49.0200, longitude: 16.9600),
9+
CLLocationCoordinate2D(latitude: 48.9000, longitude: 15.0160),
10+
CLLocationCoordinate2D(latitude: 48.2890, longitude: 13.0310),
11+
CLLocationCoordinate2D(latitude: 47.5237, longitude: 10.4350),
12+
CLLocationCoordinate2D(latitude: 46.4000, longitude: 12.1500),
13+
CLLocationCoordinate2D(latitude: 46.8700, longitude: 16.5900),
14+
CLLocationCoordinate2D(latitude: 48.1234, longitude: 16.9600),
15+
CLLocationCoordinate2D(latitude: 49.0200, longitude: 16.9600), // Closing point (same as start)
16+
]

0 commit comments

Comments
 (0)