Skip to content

Instantly share code, notes, and snippets.

@DwainTR
Created August 20, 2015 11:53
Show Gist options
  • Save DwainTR/c7181995628d5ffacba2 to your computer and use it in GitHub Desktop.
Save DwainTR/c7181995628d5ffacba2 to your computer and use it in GitHub Desktop.
Displays User's Current Location
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet var map: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var currentLocation = locations[0] as! CLLocation
var latitude = currentLocation.coordinate.latitude
var longtitude = currentLocation.coordinate.longitude
var latdelta: CLLocationDegrees = 0.001
var longdelta: CLLocationDegrees = 0.001
var span: MKCoordinateSpan = MKCoordinateSpanMake(latdelta, longdelta)
var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longtitude)
var region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
self.map.setRegion(region, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment