Last active
October 27, 2015 01:36
-
-
Save jrmullins/3e6bf1da189955565f88 to your computer and use it in GitHub Desktop.
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
import UIKit | |
import CoreLocation | |
import MapKit | |
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { | |
@IBOutlet weak var theMap: MKMapView! | |
@IBOutlet weak var theLabel: UILabel! | |
var manager:CLLocationManager! | |
var myLocations: [CLLocation] = [] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
//Setup our Location Manager | |
manager = CLLocationManager() | |
manager.delegate = self | |
manager.desiredAccuracy = kCLLocationAccuracyBest | |
manager.requestAlwaysAuthorization() | |
manager.startUpdatingLocation() | |
//Setup our Map View | |
theMap.delegate = self | |
theMap.mapType = MKMapType.Satellite | |
theMap.showsUserLocation = true | |
} | |
func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) { | |
theLabel.text = "\(locations[0])" | |
myLocations.append(locations[0] as CLLocation) | |
let spanX = 0.007 | |
let spanY = 0.007 | |
var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY)) | |
theMap.setRegion(newRegion, animated: true) | |
if (myLocations.count > 1){ | |
var sourceIndex = myLocations.count - 1 | |
var destinationIndex = myLocations.count - 2 | |
let c1 = myLocations[sourceIndex].coordinate | |
let c2 = myLocations[destinationIndex].coordinate | |
var a = [c1, c2] | |
var polyline = MKPolyline(coordinates: &a, count: a.count) | |
theMap.addOverlay(polyline) | |
} | |
} | |
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { | |
if overlay is MKPolyline { | |
var polylineRenderer = MKPolylineRenderer(overlay: overlay) | |
polylineRenderer.strokeColor = UIColor.blueColor() | |
polylineRenderer.lineWidth = 4 | |
return polylineRenderer | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment