Created
May 23, 2017 02:05
-
-
Save chrislzm/577485949b474301ef28804a55b7ae62 to your computer and use it in GitHub Desktop.
Drawing a poly line on a mapview in Swift 3
This file contains 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 somewhereElse() { | |
let startLocation = CLLocation(latitude: startLat, longitude: startLon) | |
let endLocation = CLLocation(latitude: endLat, longitude: endLon) | |
let locations = [startLocation,endLocation] | |
var coordinates = locations.map({(location: CLLocation!) -> CLLocationCoordinate2D in return location.coordinate}) | |
let polyline = MKPolyline(coordinates: &coordinates, count: locations.count) | |
mapView.add(polyline) | |
} | |
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { | |
if overlay.isKind(of: MKPolyline.self) { | |
// draw the track | |
let polyLine = overlay | |
let polyLineRenderer = MKPolylineRenderer(overlay: polyLine) | |
polyLineRenderer.strokeColor = UIColor.blue | |
polyLineRenderer.lineWidth = 2.0 | |
return polyLineRenderer | |
} | |
return MKPolylineRenderer() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment