Search code examples
swiftmapkitmkmapviewrenderingpolyline

Polyline not showing in MapKit [Xcode 9.0 - Swift 4.0]


Ive made a view controller which tracks my location once the view is opened. Once the routeButton is pressed, I want the blue Polyline to show. However it does not. Ive set the delegates and wrote the rendering function but it still does not show. I don't get an error when calculating directions. It does run the self.map.add(route.polyline, level: .aboveRoads) line. But no polyline is added to the map. Ive read multiple questions relating to this on here, but nothing fixes my issue. Any help would be greatly appreciated.

Heres my code:

class ThirdViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{


var startLocCoord = CLLocation()
var endLocCoord = CLLocation()



func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
       let location = locations[0]
       let span:MKCoordinateSpan = MKCoordinateSpanMake(0.05,0.05)
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        map.setRegion(region, animated: true)
        self.map.showsUserLocation = true
        }   



 @IBAction func routeButton (_ sender: Any) {   
    let destinationLoc = CLLocationCoordinate2D(latitude: endLocCoord.coordinate.latitude, longitude: endLocCoord.coordinate.longitude)
        let sourceLoc = CLLocationCoordinate2D(latitude: startLocCoord.coordinate.latitude, longitude: startLocCoord.coordinate.longitude)
        let sourcePlaceMark = MKPlacemark(coordinate: sourceLoc)
        let destinationPlaceMark = MKPlacemark(coordinate: destinationLoc)

        let directionRequest = MKDirectionsRequest()
        directionRequest.source = MKMapItem(placemark: sourcePlaceMark)
        directionRequest.destination = MKMapItem(placemark: destinationPlaceMark)
        directionRequest.transportType = .automobile

        let directions = MKDirections(request: directionRequest)
        directions.calculate { (response, error) in
            guard let directionResponse = response else {
                if let error = error {
                    print("Error with directions==\(error.localizedDescription)")
                }
                return
            }
            let route = directionResponse.routes[0]
            print("route ------->", route)
            self.map.add(route.polyline, level: .aboveRoads)
            let rect = route.polyline.boundingMapRect
            self.map.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
            print(self.srcLat, self.srcLon, self.lat, self.long)
 }


override func viewDidLoad() {

    manager.desiredAccuracy = kCLLocationAccuracyBest
    UIApplication.shared.isIdleTimerDisabled = true
    manager.delegate = self
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
    manager.allowsBackgroundLocationUpdates = true
    manager.pausesLocationUpdatesAutomatically = false
    self.map.isZoomEnabled = false
    self.map.isScrollEnabled = false
    self.map.delegate = self
    map.mapType = MKMapType.standard
}



func mapView(_mapView: MKMapView, rendererFor overlay: MKOverlay)-> MKOverlayRenderer{
    let renderer = MKPolylineRenderer(overlay: overlay)
    renderer.strokeColor = UIColor.blue
    renderer.lineWidth = 5.0
    print("render called")
    return renderer
}


}

Thanks


Solution

  • It's probably just a typo. Replace

    func mapView(_mapView: MKMapView, rendererFor overlay: MKOverlay)-> MKOverlayRenderer{
    

    with

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {