[英]Polyline not showing in swiftUI
Hi everyone I've been working for a few days to show a straight line on my map.大家好,我已经工作了几天来在我的 map 上显示一条直线。 I use
swiftUI
and mapkit
to render the map
.我使用
swiftUI
和mapkit
来渲染map
。 what I want to achieve is a straight line
between the two annotations, these are shown on the map
.我想要实现的是两个注释之间的
straight line
,这些显示在map
上。
Dit is de code die ik op dit moment heb. Dit 是解码代码 die ik op dit moment heb。 Ik hoop dut jullie mij kunnen helpen want ik kom er niet uit.
Ik hoop dut jullie mij kunnen helpen 想要 ik kom er niet uit。
import MapKit
struct MapViewWorldDetail: UIViewRepresentable {
var StartCoordinate: CLLocationCoordinate2D
var EndCoordinate: CLLocationCoordinate2D
var name: String
@Binding var region: CLLocationCoordinate2D
func makeUIView(context: Context) -> MKMapView {
MKMapView(frame: .zero)
}
func updateUIView(_ view: MKMapView, context: Context) {
let span = MKCoordinateSpan(latitudeDelta: 50, longitudeDelta: 50)
let region = MKCoordinateRegion(center: self.region, span: span)
// view.mapType = MKMapType.satelliteFlyover;
view.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = StartCoordinate
annotation.title = name
view.addAnnotation(annotation)
let annotationEnd = MKPointAnnotation()
annotationEnd.coordinate = EndCoordinate
annotationEnd.title = name
view.addAnnotation(annotationEnd)
let aPolyline = MKGeodesicPolyline(coordinates: [StartCoordinate, EndCoordinate], count: 2)
view.addOverlay(aPolyline)
}
}
Note on the straight line you are drawing: The line that you are drawing using MKGeodesicPolyline
has this note in the Apple Developer Documentation:注意您正在绘制的直线:您使用
MKGeodesicPolyline
绘制的直线在 Apple 开发人员文档中有此注释:
MKGeodesicPolyline
- When displayed on a two-dimensional map view, the line segment between any two points may appear curved.MKGeodesicPolyline
- 当显示在二维 map 视图上时,任意两点之间的线段可能会出现弯曲。
Example of working code工作代码示例
In SwiftUI, you'll need to implement the MKMapViewDelegate
in a Coordinator
class, which is where the Overlay handling is taken care of:在 SwiftUI 中,您需要在
Coordinator
class 中实现MKMapViewDelegate
,这是处理 Overlay 处理的地方:
func updateUIView
func updateUIView
func updateUIView(_ view: MKMapView, context: UIViewRepresentableContext<MapView>) {
// Stuff you already had in updateUIView
// :
// adding this at the end is sufficient
mapView.delegate = context.coordinator
}
struct MapViewWorldDetail
struct MapViewWorldDetail
// MARK: - Coordinator for using UIKit inside SwiftUI.
func makeCoordinator() -> MapView.Coordinator {
Coordinator(self)
}
final class Coordinator: NSObject, MKMapViewDelegate {
var control: MapView
init(_ control: MapView) {
self.control = control
}
// MARK: - Managing the Display of Overlays
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
print("mapView(_:rendererFor:)")
if let polyline = overlay as? MKPolyline {
let polylineRenderer = MKPolylineRenderer(overlay: polyline)
polylineRenderer.strokeColor = .red
polylineRenderer.lineWidth = 3
return polylineRenderer
}
return MKOverlayRenderer(overlay: overlay)
}
}
Good references to review:很好的评论参考:
Sample project that implements the Coordinator class & delegates (but does not do overlays)实现协调器 class 和代表的示例项目(但不做覆盖)
Medium posts by others其他人的中等帖子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.