首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKMapView中的覆盖MKMapView问题(rendererForOverLay)

MKMapView中的覆盖MKMapView问题(rendererForOverLay)
EN

Stack Overflow用户
提问于 2016-02-09 22:04:06
回答 1查看 1.8K关注 0票数 1

我试图为MapView上从位置A到位置B的路由添加一条折线(这是当前用户的GPS位置),因此路由/ polyline将能够从一个设置的位置A到用户当前的位置(位置B)。

现在我的覆盖不起作用了。我在MKPolylineView上查看了其他几个这样的线程,但是当我尝试实现它们的代码时(也在下面),路由/行仍然没有显示出来。我是iOS的新手,所以我自己还在熟悉Swift/mapKit。

以下是我到目前为止所拥有的:(修改以显示重要部分)

代码语言:javascript
复制
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
}

我还找到了使用MKDirections的另一个示例,它似乎更理想,因为它允许我设置传输类型(MKDirectionsTransportType.Walking)。不过,我也很难用这些指示画出路线。

使用第二组指令,以下是解决Xcode提醒我注意的一些错误后得到的信息:

代码语言:javascript
复制
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链接错了,因为在这两种情况下都没有调用它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-12 02:16:41

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

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

代码语言:javascript
复制
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35303113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档