Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

draw line between 2 points - no line?!

jarvjarv Posts: 210Registered Users @ @
edited April 2017 in iOS SDK Development
ok so I have linked up a button to display a line between 2 points, when I tap the button the map zooms into the area between the 2 points SOURCE and DESTINATION, I am expecting to see a thick blue line though.... its not showing?! please help?


@IBAction func showRoute(_ sender: Any) {
        let sourceLocation = CLLocationCoordinate2D(latitude: Double(venue.rsLat) ?? 0.0, longitude: Double(venue.rsLong) ?? 0.0)
        let destinationLocation = CLLocationCoordinate2D(latitude: (self.manager.location?.coordinate.latitude)!, longitude: (self.manager.location?.coordinate.longitude)!)
        print("Source: \(sourceLocation)")
        print("Source: \(destinationLocation)")
        let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
        let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
        let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
        let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
        let directionRequest = MKDirectionsRequest()
        directionRequest.source = sourceMapItem
        directionRequest.destination = destinationMapItem
        directionRequest.transportType = .automobile
        let directions = MKDirections(request: directionRequest)
        directions.calculate {
            (response, error) -> Void in
            guard let response = response else {
                if let error = error {
                    print("Error: \(error)")
            let route = response.routes[0]
            self.mapView.add((route.polyline), level: MKOverlayLevel.aboveRoads)
            let rect = route.polyline.boundingMapRect
            self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)

        func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
            if overlay is MKPolyline {
                var polylineRenderer = MKPolylineRenderer(overlay: overlay)
                polylineRenderer.strokeColor =
                polylineRenderer.lineWidth = 5
                return polylineRenderer
            return nil


Sign In or Register to comment.