【问题标题】:MapKit Draw poly line from coordinates Swift 5MapKit从坐标Swift 5绘制折线
【发布时间】:2019-09-19 15:02:27
【问题描述】:

我有一个 CLLocationCoordinate2D 数组,需要画一条线连接数组中的第一个索引和最后一个索引。

我已经在这里和其他地方看到了答案,但它们主要适用于旧版本的 swift,或者适用于您首先转换为双精度或其他任何内容的字符串数组,或者仅适用于单个位置。

我尝试过创建 MKPolyline、viewForOverlay 等,但无法显示该线。我错过了什么?

let locationManager = CLLocationManager()
let regionRadius: CLLocationDistance = 3000
var locations: [CLLocationCoordinate2D] = []
var latitude: [CLLocationDegrees] = []
var longitude: [CLLocationDegrees] = []

override func viewDidLoad() {
        super.viewDidLoad()
        mapView.delegate = self
        mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
        createAnnotations()
        centerMapOnLocation(location: initialLocation)
        var polyline = MKPolyline(coordinates: &locations, count: locations.count)
        mapView.addOverlay(polyline)
    }
    // MARK: - Create Annotaions
    func createAnnotations() {
        locations = zip(latitude, longitude).map(CLLocationCoordinate2D.init)
        AppLogger.logInfo("\(locations)")
        for location in locations {
            let annotation = MKPointAnnotation()
            annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
            mapView.addAnnotation(annotation)
        }
    }
    // MARK: - Region Zoom
    func centerMapOnLocation(location: CLLocation) {
        let coordinateRegion = MKCoordinateRegion(center: location.coordinate,
                                                  latitudinalMeters: regionRadius,
                                                  longitudinalMeters: regionRadius)
      mapView.setRegion(coordinateRegion, animated: true)
    }
    // MARK: - Draw Polylines
    func mapView(mapView: MKMapView!, viewForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if (overlay is MKPolyline) {
            let pr = MKPolylineRenderer(overlay: overlay)
            pr.strokeColor = UIColor.blue.withAlphaComponent(0.5)
            pr.lineWidth = 5
            return pr
        }
        return nil
    }

预期输出:从位置数组中,我们基本上绘制了行驶路线。这不是获取路线的方法,因为驱动器已经被占用了。

【问题讨论】:

    标签: swift mapkit mkpolyline


    【解决方案1】:

    使用 Swift 5

    首先,您需要使用 CLLocationCoordinate2D 数组声明 MKPolyline:

    let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
    mapView.addOverlay(polyline)
    

    然后在您的 MKMapViewDelegate 上,您需要向折线添加颜色,如下例所示:

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    
        if let routePolyline = overlay as? MKPolyline {
            let renderer = MKPolylineRenderer(polyline: routePolyline)
            renderer.strokeColor = UIColor.mySpecialBlue().withAlphaComponent(0.9)
            renderer.lineWidth = 7
            return renderer
        } 
    
        return MKOverlayRenderer()
    }
    

    通过这种方法,您将能够重新创建如下内容:

    【讨论】:

      【解决方案2】:

      首先,确认您的locations 是否为空数组。

      其次,viewForOverlay 是一个不再使用的旧方法(即使那样,方法签名也不正确)。

      相反,将创建渲染器的代码放在rendererFor

      func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
          ...
      }
      

      【讨论】:

      • 嘿,罗伯!我可以通过调试器确认数组不为空。它以双打开始,但我将它们转换为 CLLocationCoordinate2D 数组。返回 nil 时出现错误。如果我返回一个 MKOverlayRenderer,它也会给我一个错误,类型为 MKOverlayRenderer.Type 的表达式
      • 奇怪!!....我得到了覆盖,但它绘制的路线错误。它似乎来自位置而不是位置索引。这意味着它似乎正在绘制到最近的点..wich 可能不是数组中的下一个实际点。阵列是驱动器从开始到结束的位置。我会做一些调查。
      • 恢复您的调试信息,太好了。我实际上假设了你的last question,但为了未来的读者没有看到这一点,我不得不提一下。当您提供“预期输出”时,如果您还可以分享“实际输出”是什么总是好的。 ;)
      • 知道了!嘿,知道为什么它可能会错误地绘制“路线”吗?我知道覆盖不是实际路线的表示,但是,它似乎连接了最近的注释而不是索引注释。想象一下开车穿过一个单向街道的社区,而折线告诉你你开错了路……哈哈!!
      • 重新排序不是你所期望的,我敢打赌数组中的顺序是它在折线中呈现的顺序。这是一个非常强大的机制,它不会对你重新排序。我想知道您是否有一些临时的无序结构(例如,使用字典或集合,添加数据的顺序无关紧要,生成的集合是无序的,即可能以任何顺序出现)。如果没有找到,请分享minimal yet reproducible example
      猜你喜欢
      • 2017-10-28
      • 2021-05-02
      • 2018-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多