如何在Swift中使用MKPolylineView [英] How to use MKPolylineView in Swift

查看:218
本文介绍了如何在Swift中使用MKPolylineView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to draw polyline in my Swift app.

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>

我找不到解决方法。

推荐答案

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

First, instead of MKPolylineView, you must create an MKPolylineRenderer.

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

MKPolylineView has been deprecated since iOS 7 and, although you can still use it in Objective-C if necessary, it's not supported in Swift.

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

Second, you must create and return an MKPolylineRenderer in the rendererForOverlay delegate method (not pass it to addOverlay).

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

In the addOverlay method, you pass the MKPolyline object (not the MKPolylineView or MKPolylineRenderer).

(参见将MKOverlayPathRenderer作为叠加添加到MKMapView会获得异常,以解释传递给 addOverlay 的对象与<$ c $>中返回的对象之间的区别c> rendererForOverlay 。)

(See Adding MKOverlayPathRenderer as overlay to MKMapView gets exception for an explanation of the difference between what object you pass to addOverlay vs. what object you return in rendererForOverlay.)



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


So in the setMapView method, remove the lines that create and set myPolylineView and change the addOverlay line to:

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
}

确保设置了地图视图的委托,否则将不会调用委托方法并且不会显示叠加层。如果 theMapView 是一个IBOutlet,请连接委托出口或在代码中设置它(例如在 viewDidLoad 在调用super之后):

Make sure the map view's delegate is set otherwise the delegate method won't get called and the overlay will not appear. If theMapView is an IBOutlet, connect the delegate outlet or set it in code (eg. in viewDidLoad after calling super):

self.theMapView.delegate = self

这篇关于如何在Swift中使用MKPolylineView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆