Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWIFT Mapview MKRoutePolyline interiorPolygons: unrecognized selector sent to instance 0x14ed4b4d0

Tags:

ios

swift2

I'm trying to make an Navigation app

But this app crash when I trying to Navigate and show "MKRoutePolyline

interiorPolygons: unrecognized selector sent to instance 0x14ed4b4d0"

whats wrong with my code?

import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
var farm:Farm!

var currentPlacemark:CLPlacemark?

let locationManager = CLLocationManager()
var currentTransportType = MKDirectionsTransportType.Automobile
var currentRoute:MKRoute?




@IBOutlet var mapView:MKMapView!
@IBAction func showDirection(sender: AnyObject){
    let directionRequest = MKDirectionsRequest()

    directionRequest.source = MKMapItem.mapItemForCurrentLocation()
    let destinationPlacemark = MKPlacemark(placemark: currentPlacemark!)
    directionRequest.destination = MKMapItem(placemark: destinationPlacemark)
    directionRequest.transportType = currentTransportType

    let directions = MKDirections(request: directionRequest)

    directions.calculateDirectionsWithCompletionHandler { (routeResponse, routeError) -> Void in

        guard let routeResponse = routeResponse else {
            if let routeError = routeError {
                print("Error: \(routeError)")
            }

            return
        }

        let route = routeResponse.routes[0]
        self.currentRoute = route
        self.mapView.removeOverlays(self.mapView.overlays)
        self.mapView.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)



    }
}


func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
    let renderer = MKPolygonRenderer(overlay: overlay)
    renderer.strokeColor = UIColor.yellowColor()
    renderer.lineWidth = 3.0

    return renderer

}
like image 535
Johnny Hsieh Avatar asked Feb 25 '16 13:02

Johnny Hsieh


1 Answers

It seems that you want to render a line.
but polygon is a shape
so your code should be:

let renderer = MKPolylineRenderer(overlay: overlay)

But not:

let renderer = MKPolygonRenderer(overlay: overlay)

in

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer
like image 194
TurboExtension Avatar answered Sep 22 '22 23:09

TurboExtension