Skip to content

Commit

Permalink
Precipitations: Snow and Rain (#2360)
Browse files Browse the repository at this point in the history
  • Loading branch information
OdNairy authored Nov 26, 2024
1 parent 34313b2 commit 2ae693c
Show file tree
Hide file tree
Showing 17 changed files with 1,235 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Apps/Examples/Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
F5E96E5798947CA56FD77CF9 /* Fire_Hydrants.geojson in Resources */ = {isa = PBXBuildFile; fileRef = 02DA2CC04980F807255D646B /* Fire_Hydrants.geojson */; };
F613749DCDDDDC6F041032A0 /* SimpleMapExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78811E5A3185D2D32495870A /* SimpleMapExample.swift */; };
F6E3EF9BE4F1D2F58DE1BED2 /* radar1.gif in Resources */ = {isa = PBXBuildFile; fileRef = 8BD8BADE1108B0D380D9BEF8 /* radar1.gif */; };
FA077DC5A6CF295906536DF1 /* PrecipitationsExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFDBCA79855B58FE84333124 /* PrecipitationsExample.swift */; };
FA53EEA88DB29D4D5AC58514 /* StandardInteractiveFeaturesExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0FBAFDF929532EDB964786F4 /* StandardInteractiveFeaturesExample.swift */; };
FDA4B57BE32D92BB57A5B7E6 /* FeaturesAtPointExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6239A5CDA61892902765B843 /* FeaturesAtPointExample.swift */; platformFilters = (ios, ); };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -287,6 +288,7 @@
B33F64CDBA98B91EE819B2C4 /* AddMarkersSymbolExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddMarkersSymbolExample.swift; sourceTree = "<group>"; };
B7A7586D05B960928AB17A0D /* MultipleGeometriesExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultipleGeometriesExample.swift; sourceTree = "<group>"; };
BE18E37A8652B4807D2459F1 /* ExamplesUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExamplesUITests.swift; sourceTree = "<group>"; };
BFDBCA79855B58FE84333124 /* PrecipitationsExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrecipitationsExample.swift; sourceTree = "<group>"; };
C0CC67084BA1191D0B179A94 /* PuckPlayground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PuckPlayground.swift; sourceTree = "<group>"; };
C47942F80A50166AC823012B /* sportcar.glb */ = {isa = PBXFileReference; path = sportcar.glb; sourceTree = "<group>"; };
C61CC711054A032EE0446036 /* DynamicStylingExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DynamicStylingExample.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -402,6 +404,7 @@
DD3A816C6E4D7A0A532EEE84 /* GeofencingUserLocation.swift */,
62DA0608D44DEF6C4A82777C /* LocateMeExample.swift */,
45B39AE24486FED5ED30392D /* LocationOverrideExample.swift */,
BFDBCA79855B58FE84333124 /* PrecipitationsExample.swift */,
6CD7ADCCB774239AA0090C46 /* RasterParticleExample.swift */,
AC5A8729C9AEA4711B56B5F0 /* SnapshotMapExample.swift */,
E97E057D9FFB1DE3E16F1F0B /* StandardInteractiveBuildingsExample.swift */,
Expand Down Expand Up @@ -723,6 +726,7 @@
mainGroup = AFDB1EA82615CFDF02CE1D4D;
packageReferences = (
B50D5CC28BF0DFBA55456D89 /* XCRemoteSwiftPackageReference "Fingertips" */,
F97B927925CFCEE0B8496DE3 /* XCLocalSwiftPackageReference "../../" */,
);
projectDirPath = "";
projectRoot = "";
Expand Down Expand Up @@ -904,6 +908,7 @@
F48BF087BB56B0A44D8B16F3 /* PointAnnotationClusteringExample.swift in Sources */,
4417BB8A356335BC8421A19B /* PointClusteringExample.swift in Sources */,
6661DB69D4980E24BCA18AB2 /* PolygonAnnotationExample.swift in Sources */,
FA077DC5A6CF295906536DF1 /* PrecipitationsExample.swift in Sources */,
B304BACFCD08802A740E8919 /* PuckPlayground.swift in Sources */,
0E191B29AE31584DCFDC3821 /* RasterColorExample.swift in Sources */,
F492E2C8D35572B4CF48FE68 /* RasterParticleExample.swift in Sources */,
Expand Down Expand Up @@ -1339,6 +1344,13 @@
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCLocalSwiftPackageReference section */
F97B927925CFCEE0B8496DE3 /* XCLocalSwiftPackageReference "../../" */ = {
isa = XCLocalSwiftPackageReference;
relativePath = ../../;
};
/* End XCLocalSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
0AF5F744C6369BF1FB233FB6 /* MapboxMaps */ = {
isa = XCSwiftPackageProductDependency;
Expand Down
101 changes: 101 additions & 0 deletions Apps/Examples/Examples/SwiftUI Examples/PrecipitationsExample.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import SwiftUI
@_spi(Experimental) import MapboxMaps

@available(iOS 14.0, *)
struct PrecipitationExample: View {
@State var viewport: Viewport = .camera(center: CLLocationCoordinate2D(latitude: 37.33464837343596, longitude: -122.00896178062911), zoom: 18, pitch: 80)

@State var snowState: PrecipitationState = .none
@State var rainState: PrecipitationState = .none

enum PrecipitationState: CaseIterable {
case none, light, medium, heavy

var intensity: Double {
switch self {
case .none: return 0
case .light: return 0.2
case .medium: return 0.6
case .heavy: return 1.0
}
}

var opacity: Double {
switch self {
case .none: return 0
case .light: return 0.3
case .medium: return 0.5
case .heavy: return 0.8
}
}

var snowIcon: String {
switch self {
case .none: return "snowflake"
case .light: return "cloud.hail"
case .medium: return "cloud.hail.fill"
case .heavy: return "cloud.snow.fill"
}
}

var rainIcon: String {
switch self {
case .none: return "drop"
case .light: return "cloud.drizzle"
case .medium: return "cloud.drizzle.fill"
case .heavy: return "cloud.rain.fill"
}
}

mutating func toggle() {
let allCases = PrecipitationState.allCases
let currentIndex = allCases.firstIndex(of: self)!
let nextIndex = (currentIndex + 1) % allCases.count
self = allCases[nextIndex]
}
}

var body: some View {
Map(viewport: $viewport) {
Puck2D(bearing: .heading)
if snowState != .none {
Snow()
.intensity(snowState.intensity)
.opacity(snowState.opacity)
.vignette(0.5)
}
if rainState != .none {
Rain()
.intensity(rainState.intensity)
.opacity(rainState.opacity)
.vignette(0.2)
.color(.blue.withAlphaComponent(0.4))
}
}
.mapStyle(.standard)
.ignoresSafeArea()
.safeOverlay(alignment: .trailing) {
VStack {
Button {
snowState.toggle()
} label: {
Image(systemName: snowState.snowIcon)
}
.buttonStyle(MapFloatingButtonStyle())
Button {
rainState.toggle()
} label: {
Image(systemName: rainState.rainIcon)
}
.buttonStyle(MapFloatingButtonStyle())
}
}
}
}

@available(iOS 14.0, *)
struct PrecipitationExample_Preview: PreviewProvider {
static var previews: some View {
PrecipitationExample()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ struct StandardStyleImportExample: View {
}
}

@available (iOS 14.0, *)
@available(iOS 14.0, *)
private struct HotelCallout: View {
var feature: FeaturesetFeature

Expand Down
1 change: 1 addition & 0 deletions Apps/Examples/Examples/SwiftUI Examples/SwiftUIRoot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct SwiftUIRoot: View {
ExampleLink("Attribution dialog with presented sheet", destination: AttributionDialogueWithSheet())
}
#endif
ExampleLink("Precipitation", note: "Show show and rain", destination: PrecipitationExample())

} header: { Text("Testing Examples") }
}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ That initializer doesn't require to wrap arguments in `Argument` cases. For exam
* Add new experimental `radius` parameter to `TapInteraction`, `LongPressInteraction` and interaction managers to control the radius of a tappable area.
* Add a way to specify image expression options.
* Bump core maps version to 11.9.0-beta.1 and common sdk to 24.9.0-beta.1
* Add new experimental APIs to control precipitation rendering. Snow and Rain are available now with an `@_spi(Experimental)` import prefix.

## 11.8.0 - 11 November, 2024

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ struct MapContentUniqueProperties: Decodable {
var terrain: Terrain?
var atmosphere: Atmosphere?
var projection: StyleProjection?
var snow: Snow?
var rain: Rain?
var transition: TransitionOptions?
var location: LocationOptions?
var lights = Lights()
Expand All @@ -38,6 +40,8 @@ struct MapContentUniqueProperties: Decodable {
update("atmosphere", old: old.atmosphere, new: atmosphere, initial: initial?.atmosphere, setter: style.setStyleAtmosphereForProperties(_:))
update("projection", old: old.projection, new: projection, initial: initial?.projection, setter: style.setStyleProjectionForProperties(_:))
update("terrain", old: old.terrain, new: terrain, initial: initial?.terrain, setter: style.setStyleTerrainForProperties(_:))
update("snow", old: old.snow, new: snow, initial: initial?.snow, setter: style.setStyleSnowForProperties(_:))
update("rain", old: old.rain, new: rain, initial: initial?.rain, setter: style.setStyleRainForProperties(_:))

lights.update(from: old.lights, style: style, initialLights: initial?.lights)

Expand All @@ -59,6 +63,8 @@ extension MapContentUniqueProperties {
case terrain
case atmosphere = "fog"
case projection
case snow
case rain
case lights = "lights"
}

Expand All @@ -68,6 +74,8 @@ extension MapContentUniqueProperties {
self.terrain = try container.decodeIfPresent(Terrain.self, forKey: .terrain)
self.atmosphere = try container.decodeIfPresent(Atmosphere.self, forKey: .atmosphere)
self.projection = try container.decodeIfPresent(StyleProjection.self, forKey: .projection)
self.snow = try container.decodeIfPresent(Snow.self, forKey: .snow)
self.rain = try container.decodeIfPresent(Rain.self, forKey: .rain)
if var lightContainer = try? container.nestedUnkeyedContainer(forKey: .lights) {
while !lightContainer.isAtEnd {
var lightInfoContainer = lightContainer
Expand Down
2 changes: 2 additions & 0 deletions Sources/MapboxMaps/Documentation.docc/API Catalogs/Style.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
- ``StyleProjectionName``
- ``CancelError``
- ``TransitionOptions-struct``
- ``Rain``
- ``Snow``

### Declarative Map Styling

Expand Down
Loading

0 comments on commit 2ae693c

Please sign in to comment.