Last active
June 18, 2017 14:13
-
-
Save ozgurshn/aef669e51c4fb649edb37ebd1cecc298 to your computer and use it in GitHub Desktop.
Draw route between two core location point
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func drawRoutes(sourceLocation:CLLocationCoordinate2D , destinationLocation:CLLocationCoordinate2D) | |
{ | |
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)") | |
} | |
return | |
} | |
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment