Post

Replies

Boosts

Views

Activity

Reply to SwiftUI MapKit
Here is a code snippet that lets you tap 2 points on the map to calculate a route and verify that MapPolyline is still rendered below the traffic layer in certain cities, despite using .mapOverlayLevel(level: .aboveRoads). Try highways import SwiftUI import MapKit struct ContentView: View { @State private var position: MapCameraPosition = .region(MKCoordinateRegion( center: CLLocationCoordinate2D(latitude: 51.5, longitude: -0.1), latitudinalMeters: 2000, longitudinalMeters: 2000 )) @State private var tappedCoordinates: [CLLocationCoordinate2D] = [] @State private var routePolyline: MKPolyline? = nil @State private var isCalculating = false var body: some View { ZStack(alignment: .bottom) { MapReader { proxy in Map(position: $position) { if let polyline = routePolyline { MapPolyline(polyline) .stroke(.blue, lineWidth: 10) .mapOverlayLevel(level: .aboveRoads) } ForEach(tappedCoordinates.indices, id: \.self) { i in Marker("\(i + 1)", coordinate: tappedCoordinates[i]) .tint(i == 0 ? .green : .red) } } .mapStyle(.standard(showsTraffic: true)) .onTapGesture { screen in guard let coord = proxy.convert(screen, from: .local) else { return } if tappedCoordinates.count < 2 { tappedCoordinates.append(coord) } } } VStack(spacing: 12) { if tappedCoordinates.count == 2 { Button(isCalculating ? "Calculating..." : "Calculate Route") { Task { await calculateRoute() } } .buttonStyle(.borderedProminent) .disabled(isCalculating) } Button("Clear") { tappedCoordinates = [] routePolyline = nil } .buttonStyle(.bordered) } .padding() } } private func calculateRoute() async { guard tappedCoordinates.count == 2 else { return } isCalculating = true let request = MKDirections.Request() request.source = MKMapItem(placemark: MKPlacemark(coordinate: tappedCoordinates[0])) request.destination = MKMapItem(placemark: MKPlacemark(coordinate: tappedCoordinates[1])) request.transportType = .automobile do { let directions = try await MKDirections(request: request).calculate() routePolyline = directions.routes.first?.polyline } catch { print("Route error: \(error)") } isCalculating = false } }
May ’26
Reply to SwiftUI MapKit
Negative .aboveRoads still won't place the MapPolyline below the traffic layer. There's no public API to insert a polyline between road geometry and road labels. That middle layer is what Apple Maps appears to use internally for its own route rendering, but it's private and not accessible to us... Apple Maps achieves (I guess) that look purely because it has access to an internal rendering pipeline that isn't exposed through MapKit's public APIs Tested with a simple snippet and confirmed in the same location (next to my house) Map(position: $cameraPosition) { UserAnnotation() if let route { MapPolyline(route.polyline) .stroke(.blue, style: StrokeStyle(lineWidth: 8, lineCap: .round)) .mapOverlayLevel(level: .aboveRoads) } } .mapStyle(.standard( elevation: .automatic, emphasis: .automatic, pointsOfInterest: .all, showsTraffic: true ))
May ’26
Reply to SwiftUI MapKit
May 24, 2026 at 3:21 PM – FB22848199
Replies
Boosts
Views
Activity
4w
Reply to SwiftUI MapKit
Here is a code snippet that lets you tap 2 points on the map to calculate a route and verify that MapPolyline is still rendered below the traffic layer in certain cities, despite using .mapOverlayLevel(level: .aboveRoads). Try highways import SwiftUI import MapKit struct ContentView: View { @State private var position: MapCameraPosition = .region(MKCoordinateRegion( center: CLLocationCoordinate2D(latitude: 51.5, longitude: -0.1), latitudinalMeters: 2000, longitudinalMeters: 2000 )) @State private var tappedCoordinates: [CLLocationCoordinate2D] = [] @State private var routePolyline: MKPolyline? = nil @State private var isCalculating = false var body: some View { ZStack(alignment: .bottom) { MapReader { proxy in Map(position: $position) { if let polyline = routePolyline { MapPolyline(polyline) .stroke(.blue, lineWidth: 10) .mapOverlayLevel(level: .aboveRoads) } ForEach(tappedCoordinates.indices, id: \.self) { i in Marker("\(i + 1)", coordinate: tappedCoordinates[i]) .tint(i == 0 ? .green : .red) } } .mapStyle(.standard(showsTraffic: true)) .onTapGesture { screen in guard let coord = proxy.convert(screen, from: .local) else { return } if tappedCoordinates.count < 2 { tappedCoordinates.append(coord) } } } VStack(spacing: 12) { if tappedCoordinates.count == 2 { Button(isCalculating ? "Calculating..." : "Calculate Route") { Task { await calculateRoute() } } .buttonStyle(.borderedProminent) .disabled(isCalculating) } Button("Clear") { tappedCoordinates = [] routePolyline = nil } .buttonStyle(.bordered) } .padding() } } private func calculateRoute() async { guard tappedCoordinates.count == 2 else { return } isCalculating = true let request = MKDirections.Request() request.source = MKMapItem(placemark: MKPlacemark(coordinate: tappedCoordinates[0])) request.destination = MKMapItem(placemark: MKPlacemark(coordinate: tappedCoordinates[1])) request.transportType = .automobile do { let directions = try await MKDirections(request: request).calculate() routePolyline = directions.routes.first?.polyline } catch { print("Route error: \(error)") } isCalculating = false } }
Replies
Boosts
Views
Activity
May ’26
Reply to SwiftUI MapKit
I still disagree. In which city did you tested this? See this
Replies
Boosts
Views
Activity
May ’26
Reply to SwiftUI MapKit
Negative .aboveRoads still won't place the MapPolyline below the traffic layer. There's no public API to insert a polyline between road geometry and road labels. That middle layer is what Apple Maps appears to use internally for its own route rendering, but it's private and not accessible to us... Apple Maps achieves (I guess) that look purely because it has access to an internal rendering pipeline that isn't exposed through MapKit's public APIs Tested with a simple snippet and confirmed in the same location (next to my house) Map(position: $cameraPosition) { UserAnnotation() if let route { MapPolyline(route.polyline) .stroke(.blue, style: StrokeStyle(lineWidth: 8, lineCap: .round)) .mapOverlayLevel(level: .aboveRoads) } } .mapStyle(.standard( elevation: .automatic, emphasis: .automatic, pointsOfInterest: .all, showsTraffic: true ))
Replies
Boosts
Views
Activity
May ’26
Reply to MapKit in SwiftUI
Example
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’26