ios - MKMapView 中的叠加 MKPolyLine 问题(rendererForOverLay)

标签 ios swift mkmapview core-location mkpolyline

我正在尝试为 MapView 上的路线添加一条从位置 A 到位置 B(这是当前用户的 GPS 位置)的折线,因此路线/折线将能够从设定的位置 A 到任何位置用户当前正在(位置 B)飞行。

现在我的叠加层不起作用。我在 MKPolylineView 上查看了其他一些 SO 线程,但是当我尝试实现他们的代码(也在下面)时,路线/线路仍然没有显示。我是 iOS 新手,所以我自己还在熟悉 Swift/mapKit。

这是我目前所拥有的:(已修改以显示重要部分)

class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var setLocButton: UIButton!
@IBOutlet weak var mapView: MKMapView!

var locationManager: CLLocationManager = CLLocationManager()
var carLat = 36.136111, carLong = -80.279462


func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]!) { //AnyObject->CLLocation
    var latestLocation: CLLocation = locations[locations.count - 1]

    //Displaying location A on mapView
    let carCoords = CLLocationCoordinate2D(latitude: carLat, longitude: carLong)
    let region = MKCoordinateRegion(center: carCoords, span: MKCoordinateSpan(latitudeDelta: 0.0035, longitudeDelta: 0.0035))
    mapView.mapType = MKMapType.Hybrid
    mapView.setRegion(region, animated: true)

    //Attempting to display route information on mapView
    mapView.showsUserLocation = true
    var locations = [CLLocation(latitude: carLat, longitude: carLong), CLLocation(latitude: latestLocation.coordinate.latitude, longitude: latestLocation.coordinate.latitude)]
    var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})
    var polyline = MKPolyline(coordinates: &coordinates, count: locations.count)

    self.mapView.addOverlay(polyline)

}

//rendererForOverlay
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
    if overlay is MKPolyline {

        /* Does not get called */
        print("rendererForOverlay") 

        var polylineRenderer = MKPolylineRenderer(overlay: overlay)
        polylineRenderer.strokeColor = UIColor.blueColor()
        polylineRenderer.lineWidth = 5
        return polylineRenderer
    }
    return nil
}

我还找到了another example using MKDirections ,这似乎更理想,因为它允许我设置传输类型 (MKDirectionsTransportType.Walking)。不过,我在使用这些说明绘制路线时也遇到了问题。

使用第二组指令,这是我在解决 Xcode 提醒我的一些错误后得到的:

var route: MKRoute?

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]!) { //AnyObject->CLLocation
    var latestLocation: CLLocation = locations[locations.count - 1] //AnyObject

    /* 
    ...
    */

    //carLocation = Location A; currentLocation = location B
    var directionsRequest = MKDirectionsRequest()
    let carLocation = MKPlacemark(coordinate: carCoords, addressDictionary: nil) 
    var currentLocation = MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: coorLat, longitude: coorLong), addressDictionary: nil)

    directionsRequest.source = MKMapItem(placemark: currentLocation)
    directionsRequest.destination = MKMapItem(placemark: carLocation)
    directionsRequest.transportType = MKDirectionsTransportType.Walking

    var directions = MKDirections(request: directionsRequest)
    directions.calculateDirectionsWithCompletionHandler {
        (response, error) -> Void in
        if error == nil {
            self.route = response!.routes[0] as? MKRoute
            self.mapView.addOverlay((self.route?.polyline)!)
        }
    }
}

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {

    /* Does not get executed as well */
    print("RendererForOverLay")

    var myLineRenderer = MKPolylineRenderer(polyline: (route?.polyline)!)
    myLineRenderer.strokeColor = UIColor.redColor()
    myLineRenderer.lineWidth = 3
    return myLineRenderer
}

我是否以某种方式将 rendererForOverlay 链接错了,因为在这两种情况下都没有调用它?

最佳答案

Sofacoder 是对的,折线渲染器现在可以工作了!

我忘记在我的函数中添加 myMap.delegate = self,并且还省略了 MKMapViewDelegate ViewController 的声明:

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

关于ios - MKMapView 中的叠加 MKPolyLine 问题(rendererForOverLay),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35303113/

相关文章:

ios - 使用 Swift 搜索结果后搜索栏不重新加载到原始数据?

ios - 在 Swift 中使用框架

ios - 使用 sectionForSectionIndexTitle 在 UITableView 中快速滚动

swift - 在 Swift 中将 Float 转换为 Int

swift - swift语言中 '#'标记是什么意思

xcode - 我有 30 个注释,并且还在增加。正在寻找一种更简单的编码方法?

objective-c - 坐标区域的中心坐标

ios - UIPickerView 行为异常

swift - 为什么 IB Action 设置为 exit as object?

ios - 获取 MapView.annotations 数组中 MKAnnotation 的索引