Skip to content

Commit

Permalink
MAPSIOS-1539: Expose autoMaxZoom property on GeoJSONSource (mapbox#2274)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksproger authored Aug 29, 2024
1 parent c35f484 commit a6a4a30
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import MapboxMaps
@_spi(Experimental) import MapboxMaps
import SwiftUI

@available(iOS 14.0, *)
Expand Down Expand Up @@ -33,7 +33,9 @@ struct AnnotationsOrderTestExample: View {
circles: circles
)
TestLayer(id: "black-layer", radius: 2, color: .black.darker, coordinate: .init(latitude: -10, longitude: 0))
FadingCircle()
}
.debugOptions(.camera)
.mapStyle(mapStyle)
.onLayerTapGesture("purple-layer") { _, context in
tapMessage = gestureMessage("Purple layer", context: context)
Expand Down Expand Up @@ -233,6 +235,21 @@ private struct TestLayer: MapStyleContent {
}
}

@available(iOS 13.0, *)
private struct FadingCircle: MapStyleContent {
var body: some MapStyleContent {
GeoJSONSource(id: "source-id")
.data(.geometry(makeFadingCircle()))
.autoMaxZoom(true)

FillExtrusionLayer(id: "fill-layer-id", source: "source-id")
.fillExtrusionFloodLightGroundRadius(-400000)
.fillExtrusionColor(UIColor(red: 0, green: 0, blue: 0, alpha: 0))
.fillExtrusionFloodLightColor(.blue)
.fillExtrusionFloodLightIntensity(0.5)
}
}

private func gestureMessage(_ label: String, context: MapContentGestureContext) -> String {
let coordinate = String(format: "%.2f, %.2f", context.coordinate.latitude, context.coordinate.longitude)
return "\(label) (\(coordinate))"
Expand Down Expand Up @@ -312,3 +329,29 @@ private extension Polygon {
]
])
}

private func makeFadingCircle(
_ center: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: -37.8, longitude: 145.004),
radius: Double = 600000,
tess: Int = 3600
) -> Geometry {
let earthRadiusMeters = 6378137.0
let degToRad = Double.pi / 180.0

let lon0 = center.longitude * degToRad
let lat0 = center.latitude * degToRad

var coords = (0..<tess).map { index in
let bearing = (Double(index) * 2.0 * Double.pi) / Double(tess)
let angle = radius / earthRadiusMeters
var lat = asin(sin(lat0) * cos(angle) + cos(lat0) * sin(angle) * cos(bearing))
var lon = lon0 + atan2(sin(bearing) * sin(angle) * cos(lat0), cos(angle) - sin(lat0) * sin(lat))

lat = lat / degToRad
lon = lon / degToRad
return CLLocationCoordinate2D(latitude: lat, longitude: lon)
}

coords.append(coords[0])
return Geometry(Polygon([coords]))
}
13 changes: 13 additions & 0 deletions Sources/MapboxMaps/Style/Generated/Sources/GeoJSONSource.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a6a4a30

Please sign in to comment.