Skip to content

Instantly share code, notes, and snippets.

@chrislzm
Created May 23, 2017 02:05
Show Gist options
  • Save chrislzm/577485949b474301ef28804a55b7ae62 to your computer and use it in GitHub Desktop.
Save chrislzm/577485949b474301ef28804a55b7ae62 to your computer and use it in GitHub Desktop.
Drawing a poly line on a mapview in Swift 3
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