|
| 1 | +import SwiftUI |
| 2 | +import Turf |
| 3 | +@_spi(Experimental) import MapboxMaps |
| 4 | + |
| 5 | +/// Native implementation for the [Add an elevated line to a map](https://docs.mapbox.com/mapbox-gl-js/example/elevated-line/) |
| 6 | +/// example from GL-JS 3.8 |
| 7 | +struct ElevatedLineMapView: View { |
| 8 | + private let elevationPropertyKey = "elevation" |
| 9 | + private let lineSourceID = "geojson" |
| 10 | + private let lineLayerID = "elevated-line" |
| 11 | + |
| 12 | + var body: some View { |
| 13 | + Map(initialViewport: .camera( |
| 14 | + center: CLLocationCoordinate2D(latitude: 45.8418, longitude: 6.7782), |
| 15 | + zoom: 11, |
| 16 | + bearing: -150, |
| 17 | + pitch: 62 |
| 18 | + )) { |
| 19 | + StyleProjection(name: .mercator) |
| 20 | + |
| 21 | + createGeoJSONSource() |
| 22 | + |
| 23 | + LineLayer(id: lineLayerID, source: lineSourceID) |
| 24 | + .lineJoin(.round) |
| 25 | + .lineWidth(8) |
| 26 | + .lineColor(.blue) |
| 27 | + .lineEmissiveStrength(1.0) |
| 28 | + .lineElevationReference(.sea) |
| 29 | + .lineZOffset( |
| 30 | + Exp(.atInterpolated) { |
| 31 | + Exp(.product) { |
| 32 | + Exp(.lineProgress) |
| 33 | + Exp(.subtract, Exp(.length, Exp(.get, elevationPropertyKey)), 1) |
| 34 | + } |
| 35 | + Exp(.get, elevationPropertyKey) |
| 36 | + } |
| 37 | + ) |
| 38 | + } |
| 39 | + .mapStyle(.standard) |
| 40 | + .ignoresSafeArea(.all) |
| 41 | + } |
| 42 | + |
| 43 | + private func createGeoJSONSource() -> GeoJSONSource { |
| 44 | + var source = GeoJSONSource(id: lineSourceID) |
| 45 | + .data(createGeoJSON()) |
| 46 | + source.lineMetrics = true |
| 47 | + return source |
| 48 | + } |
| 49 | + |
| 50 | + private func createGeoJSON() -> GeoJSONSourceData { |
| 51 | + let feature = Feature(geometry: Geometry(LineString(coordinates))) |
| 52 | + .properties([ |
| 53 | + elevationPropertyKey: .array(elevations.map {JSONValue(Double($0))}) |
| 54 | + ]) |
| 55 | + |
| 56 | + return .feature(feature) |
| 57 | + } |
| 58 | + |
| 59 | + private let coordinates: [CLLocationCoordinate2D] = [ |
| 60 | + CLLocationCoordinate2D(latitude: 45.833563, longitude: 6.862885), |
| 61 | + CLLocationCoordinate2D(latitude: 45.846851, longitude: 6.863605), |
| 62 | + CLLocationCoordinate2D(latitude: 45.862445, longitude: 6.859783), |
| 63 | + CLLocationCoordinate2D(latitude: 45.876361, longitude: 6.848727), |
| 64 | + CLLocationCoordinate2D(latitude: 45.892361, longitude: 6.827155), |
| 65 | + CLLocationCoordinate2D(latitude: 45.905032, longitude: 6.802194), |
| 66 | + CLLocationCoordinate2D(latitude: 45.909602, longitude: 6.780023), |
| 67 | + CLLocationCoordinate2D(latitude: 45.906074, longitude: 6.753605), |
| 68 | + CLLocationCoordinate2D(latitude: 45.899120, longitude: 6.728807), |
| 69 | + CLLocationCoordinate2D(latitude: 45.883872, longitude: 6.700449), |
| 70 | + CLLocationCoordinate2D(latitude: 45.863866, longitude: 6.683772), |
| 71 | + CLLocationCoordinate2D(latitude: 45.841619, longitude: 6.684058), |
| 72 | + CLLocationCoordinate2D(latitude: 45.825417, longitude: 6.691115), |
| 73 | + CLLocationCoordinate2D(latitude: 45.813349, longitude: 6.704446), |
| 74 | + CLLocationCoordinate2D(latitude: 45.807886, longitude: 6.720959), |
| 75 | + CLLocationCoordinate2D(latitude: 45.809517, longitude: 6.748477), |
| 76 | + CLLocationCoordinate2D(latitude: 45.817254, longitude: 6.775554), |
| 77 | + CLLocationCoordinate2D(latitude: 45.828871, longitude: 6.791236), |
| 78 | + CLLocationCoordinate2D(latitude: 45.838797, longitude: 6.801289), |
| 79 | + CLLocationCoordinate2D(latitude: 45.849788, longitude: 6.806307), |
| 80 | + CLLocationCoordinate2D(latitude: 45.866159, longitude: 6.803161), |
| 81 | + CLLocationCoordinate2D(latitude: 45.880461, longitude: 6.794599), |
| 82 | + CLLocationCoordinate2D(latitude: 45.890231, longitude: 6.769846), |
| 83 | + CLLocationCoordinate2D(latitude: 45.889576, longitude: 6.744712), |
| 84 | + CLLocationCoordinate2D(latitude: 45.881677, longitude: 6.722788), |
| 85 | + CLLocationCoordinate2D(latitude: 45.868556, longitude: 6.708097), |
| 86 | + CLLocationCoordinate2D(latitude: 45.851973, longitude: 6.699435), |
| 87 | + CLLocationCoordinate2D(latitude: 45.832980, longitude: 6.707324), |
| 88 | + CLLocationCoordinate2D(latitude: 45.822384, longitude: 6.723743), |
| 89 | + CLLocationCoordinate2D(latitude: 45.818626, longitude: 6.739347), |
| 90 | + CLLocationCoordinate2D(latitude: 45.822069, longitude: 6.756019), |
| 91 | + CLLocationCoordinate2D(latitude: 45.832436, longitude: 6.773963), |
| 92 | + CLLocationCoordinate2D(latitude: 45.848229, longitude: 6.785920), |
| 93 | + CLLocationCoordinate2D(latitude: 45.860521, longitude: 6.786155), |
| 94 | + CLLocationCoordinate2D(latitude: 45.870586, longitude: 6.774430), |
| 95 | + CLLocationCoordinate2D(latitude: 45.875670, longitude: 6.749012), |
| 96 | + CLLocationCoordinate2D(latitude: 45.868501, longitude: 6.731251), |
| 97 | + CLLocationCoordinate2D(latitude: 45.853689, longitude: 6.716033), |
| 98 | + CLLocationCoordinate2D(latitude: 45.846970, longitude: 6.714748), |
| 99 | + CLLocationCoordinate2D(latitude: 45.838934, longitude: 6.714635), |
| 100 | + CLLocationCoordinate2D(latitude: 45.832829, longitude: 6.717850), |
| 101 | + CLLocationCoordinate2D(latitude: 45.828151, longitude: 6.724010), |
| 102 | + CLLocationCoordinate2D(latitude: 45.827333, longitude: 6.730551), |
| 103 | + CLLocationCoordinate2D(latitude: 45.829900, longitude: 6.733951), |
| 104 | + CLLocationCoordinate2D(latitude: 45.834154, longitude: 6.735957), |
| 105 | + CLLocationCoordinate2D(latitude: 45.839871, longitude: 6.735286), |
| 106 | + CLLocationCoordinate2D(latitude: 45.843933, longitude: 6.734471), |
| 107 | + CLLocationCoordinate2D(latitude: 45.847233, longitude: 6.730893), |
| 108 | + CLLocationCoordinate2D(latitude: 45.847899, longitude: 6.728550), |
| 109 | + CLLocationCoordinate2D(latitude: 45.847822, longitude: 6.726590), |
| 110 | + CLLocationCoordinate2D(latitude: 45.846455, longitude: 6.724876), |
| 111 | + CLLocationCoordinate2D(latitude: 45.843900, longitude: 6.725096), |
| 112 | + CLLocationCoordinate2D(latitude: 45.841201, longitude: 6.726635), |
| 113 | + CLLocationCoordinate2D(latitude: 45.837041, longitude: 6.728074), |
| 114 | + CLLocationCoordinate2D(latitude: 45.834292, longitude: 6.727822), |
| 115 | + ] |
| 116 | + |
| 117 | + private let elevations = [ |
| 118 | + 4600, 4600, 4600, 4599, 4598, 4596, 4593, 4590, 4584, 4578, 4569, |
| 119 | + 4559, 4547, 4533, 4516, 4497, 4475, 4450, 4422, 4390, 4355, 4316, |
| 120 | + 4275, 4227, 4177, 4124, 4068, 4009, 3946, 3880, 3776, 3693, 3599, |
| 121 | + 3502, 3398, 3290, 3171, 3052, 2922, 2786, 2642, 2490, 2332, 2170, |
| 122 | + 1994, 1810, 1612, 1432, 1216, 1000 |
| 123 | + ] |
| 124 | +} |
0 commit comments