如何绘制虚线MapPolyline

问题描述 投票:0回答:1

我尝试过,但未能弄清楚如何使用破折号格式化 MapPolyLine,使用 .lines 和 .linesStyle,但我被难住了。这是一个最小的例子。如何添加破折号的格式?谢谢!

import SwiftUI
import MapKit

struct ContentView: View {
    let routes = Routes()

    var body: some View {
        VStack {
            Map() {
                ForEach(routes, id: \.id) { route in
                    MapPolyline(coordinates: route.segment,
                                contourStyle: MapPolyline.ContourStyle.straight)
                    .stroke(route.tint, lineWidth: route.lineWidth)//, dash: route.dash)

                }
            }
        }
    }
}

struct Route: Identifiable {
    let id: String = UUID().uuidString
    var segment: [CLLocationCoordinate2D]
    var tint: Color
    var lineWidth: CGFloat
    var dash: [CGFloat]
}

func Routes() -> [Route] {
        
    var segments: [Route] = []
    
    let routeOne: Route = Route(segment: [
        CLLocationCoordinate2D(latitude: 48.10589, longitude: -122.77899),
        CLLocationCoordinate2D(latitude: 48.0995, longitude: -122.81254),
        CLLocationCoordinate2D(latitude: 48.09196, longitude: -122.81565),
        CLLocationCoordinate2D(latitude: 48.0873599, longitude: -122.84327),
        CLLocationCoordinate2D(latitude: 48.052428, longitude: -122.829886)],
                                tint: Color(.green), lineWidth: 8, dash: [] )
    segments.append(routeOne)
    
    let routeTwo = Route(segment: [
        CLLocationCoordinate2D(latitude: 48.02569, longitude: -123.01255),
        CLLocationCoordinate2D(latitude: 48.040381, longitude: -123.02743),
        CLLocationCoordinate2D(latitude: 48.05617, longitude: -123.04887),
        CLLocationCoordinate2D(latitude: 48.07843, longitude: -123.08072)],
        tint: Color(.red), lineWidth: 8, dash: [8,8] )
    segments.append(routeTwo)
    
    return segments
}

我尝试使用 .lines 修饰符但没有成功。

swiftui mapkit
1个回答
0
投票

尝试这样的事情:

.stroke(route.tint, style: StrokeStyle(lineWidth: route.lineWidth, dash: [10,5]))
© www.soinside.com 2019 - 2024. All rights reserved.