【发布时间】:2019-12-04 18:40:34
【问题描述】:
在我的应用程序中,我使用 MKMapView SwiftUI 实现。我的地图运行良好,但我想在从 ContentView 中点击按钮时获取路线。我已经在下面更详细地解释了......
这是我的内容视图:
struct ContentView: View {
var body: some View {
VStack {
AppleMapView(coordinate: CLLocationCoordinate2D(latitude: 40.7127, longitude: -74.0059))
.frame(height: 400)
Button(action: {
**// !!! I want to call AppleMapView/getDirections() method here !!!**
}) {
Text("Get Directions")
}
}
}
}
这是我的地图视图:
struct AppleMapView: UIViewRepresentable {
var coordinate: CLLocationCoordinate2D
func makeUIView(context: Context) -> MKMapView {
// some codes //
}
func updateUIView(_ uiView: MKMapView, context: Context) {
// some codes //
}
func makeCoordinator() -> Coordinator {
return Coordinator()
}
func getDirections() {
// some codes //
}
class Coordinator: NSObject, MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = .blue
renderer.lineWidth = 4
return renderer
}
}
}
谢谢。
【问题讨论】:
-
您收到什么错误?为什么不能调用那个函数?我们需要更多信息来提供帮助。
-
我已经更新了问题。我觉得比较好理解。谢谢。
-
我的回答有帮助吗?
标签: ios swift swiftui ios13 xcode11