关于ios:MKMapView中的Overlay MKPolyLine问题(rendererForOverLay)

Overlay MKPolyLine issue in MKMapView (rendererForOverLay)

我正在尝试在 MapView 上为从位置 A 到位置 B(这是当前用户的 GPS 位置)的路线添加折线,以便路线/折线能够从设定的位置跟随A 到用户当前所在的位置(位置 B)。

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 提醒我的一些错误后得到的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 的声明:

1
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/268578.html

(0)
上一篇 2022年6月20日
下一篇 2022年6月20日

相关推荐

发表回复

登录后才能评论