gpt4 book ai didi

ios - 如何在 Swift 中使用 MKPolylineView

转载 作者:IT王子 更新时间:2023-10-29 05:29:35 24 4
gpt4 key购买 nike

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

我找不到解决办法。

最佳答案

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

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

其次,您必须在 rendererForOverlay 委托(delegate)方法中创建并返回一个 MKPolylineRenderer(不要将其传递给 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
}

确保 map View 的 delegate 已设置,否则将不会调用委托(delegate)方法并且不会出现叠加层。如果 theMapView 是一个 IBOutlet,连接 delegate outlet 或在代码中设置它(例如在 viewDidLoad 调用 super 之后):

self.theMapView.delegate = self

关于ios - 如何在 Swift 中使用 MKPolylineView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24798737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com