如何在Swift中使用MKPolylineView

时间:2014-07-17 08:40:47

标签: ios swift mkpolyline

我想在我的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>

我无法找到解决方案。

1 个答案:

答案 0 :(得分:21)

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

自iOS 7以来,

MKPolylineView已被弃用,尽管如有必要,您仍然可以在Objective-C中使用它,但Swift不支持它。

第二次,您必须在MKPolylineRenderer委托方法中创建并返回rendererForOverlay(不将其传递给addOverlay)。

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

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


因此,在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