简体   繁体   English

swiftUI 中未显示折线

[英]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 .我使用swiftUImapkit来渲染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工作代码示例

MapKit 的工作示例


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 处理的地方:

  1. Add this to 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
    }
  1. Add this to your struct, 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:很好的评论参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM