【问题标题】:How to use MKPolylineView in Swift如何在 Swift 中使用 MKPolylineView
【发布时间】:2014-07-17 08:40:46
【问题描述】:

我想在我的 Swift 应用中绘制折线。

Swift 代码

class MapViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet var theMapView: MKMapView

    override func viewDidLoad() {
        super.viewDidLoad()
        setMapView()
    }

    func setMapView() {
        //theMapView.zoomEnabled = false
        //theMapView.scrollEnabled = false 
        theMapView.rotateEnabled = false  

        // 
        var lat: CLLocationDegrees = 37.586601
        var lng: CLLocationDegrees = 127.009381

        // 
        var latDelta: CLLocationDegrees = 0.008
        var lngDelta: CLLocationDegrees = 0.008

        var theSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lngDelta)

        var initLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, lng)
        var theRegion: MKCoordinateRegion = MKCoordinateRegionMake(initLocation, theSpan)
        self.theMapView.setRegion(theRegion, animated: true)

        var locations = [CLLocation(latitude: 37.582691, longitude: 127.011186), CLLocation(latitude: 37.586112,longitude: 127.011047), CLLocation(latitude: 37.588212, longitude: 127.010438)]
        var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})
        var polyline = MKPolyline(coordinates: &coordinates, count: locations.count)

        var myPolylineView : MKPolylineView
        /* error */
        myPolylineView.polyline = polyline // #1
        myPolylineView.strokeColor = UIColor.blueColor() // #2
        myPolylineView.lineWidth = 5; // #3
        self.theMapView.addOverlay(myPolylineView) // #4
        /* ----- */    
    }
}

错误:

// #1 <br>
Cannot assign to 'polyline' in 'myPolylineView' <br>
// #2 <br>
'strokeColor' is unvailable: APIs deprecated as iOS 7 and earlier are unavailable in Swift <br>
// #3 <br>
'lineWidth' is unvailable: APIs deprecated as iOS 7 and earlier are unavailable in Swift <br>
// #4 <br>
Missing argument for parameter 'level' in call <br>

我找不到解决办法。

【问题讨论】:

    标签: ios swift mkpolyline


    【解决方案1】:

    首先,您必须创建一个MKPolylineRenderer,而不是MKPolylineView

    MKPolylineView 自 iOS 7 起已被弃用,尽管必要时您仍可以在 Objective-C 中使用它,但 Swift 不支持它。

    第二,你必须在rendererForOverlay委托方法中创建并返回一个MKPolylineRenderer(而不是传递给addOverlay)。

    addOverlay 方法中,您传递MKPolyline 对象(不是MKPolylineViewMKPolylineRenderer)。

    (请参阅Adding MKOverlayPathRenderer as overlay to MKMapView gets exception,了解您传递给addOverlay 的对象与您在rendererForOverlay 中返回的对象之间的区别。)


    所以在setMapView 方法中,删除创建和设置myPolylineView 的行并将addOverlay 行更改为:

    self.theMapView.addOverlay(polyline)
    

    然后实现rendererForOverlay方法:

    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKPolyline {
            var polylineRenderer = MKPolylineRenderer(overlay: overlay)
            polylineRenderer.strokeColor = UIColor.blueColor()
            polylineRenderer.lineWidth = 5
            return polylineRenderer
        }
    
        return nil
    }
    

    确保地图视图的delegate 已设置,否则将不会调用委托方法并且不会出现覆盖。如果theMapView 是一个IBOutlet,连接delegate 插座或在代码中设置它(例如,在调用super 之后在viewDidLoad 中):

    self.theMapView.delegate = self
    

    【讨论】:

    • @VanDuTran,确保以下几点:1)地图视图出口已连接,2)地图视图委托已连接/设置,3)rendererForOverlay 方法拼写正确。如果仍未接到电话,请用您的详细信息提出一个新问题。
    • 这是因为我的geojson有一些奇怪的坐标:“坐标”:[[294636.0501,5039009.536],[294572.3566,5039049.88]]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 2021-04-05
    • 2014-09-18
    • 2017-08-05
    相关资源
    最近更新 更多