Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Paul M
16,370 PointsHow to get user's current location in Swift 2.0?
First of all, I have looked at other examples and they are not working for me. Some use a different version of swift or Xcode. And some are in objective-c. I have finished the weather app Stormy, and it uses Forecast.io and stuff. And it takes 2 values as the longitude and latitude. But one of the main features is implementing the current location. Is there anyone that can provide an easy way to retrieve the user's location via swift 2.0?
2 Answers
Andres Aguero
30,545 PointsBelow is some code that I had done to retrieve user current location on a previous project. Make sure before you run this code to go into your plist and add "NSLocationWhenInUseUsageDescription" of type String with any value.
Also add in your plist "NSLocationAlwaysUsageDescription" of type string with any value as well.
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()
let latitude:CLLocationDegrees = 33.828461
let longitude:CLLocationDegrees = -117.908934
let latDelta:CLLocationDegrees = 0.01
let lonDelta:CLLocationDegrees = 0.01
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
map.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Your Location"
annotation.subtitle = "This is a subtitle"
map.addAnnotation(annotation)
let uilpgr = UILongPressGestureRecognizer(target: self, action: Selector("action:"))
uilpgr.minimumPressDuration = 2
map.addGestureRecognizer(uilpgr)
}
func action(gestureRecognizer: UIGestureRecognizer) {
print("Gesture Recognized")
let touchPoint = gestureRecognizer.locationInView(self.map)
let newCoordinate:CLLocationCoordinate2D = map.convertPoint(touchPoint, toCoordinateFromView: self.map)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
annotation.title = "New Place"
annotation.subtitle = "One day I will own a big house"
map.addAnnotation(annotation)
}
//this function moves the map
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
let userLocation: CLLocation = locations[0]
let latitude = userLocation.coordinate.latitude
let longitude = userLocation.coordinate.longitude
let latDelta:CLLocationDegrees = 0.01
let lonDelta:CLLocationDegrees = 0.01
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
self.map.setRegion(region, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Jhoan Arango
14,575 PointsHello :
You can do something like this.
import CoreLocation
class ViewController: UIViewController {
// CoreLocation Instance
let locationManager = CLLocationManager()
override func viewDidLoad(){
super.viewDidLoad()
findMyLocation()
}
// Helper Method
func findMyLocation() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.requestWhenInUseAuthorization()
// Here we start locating
locationManager.startUpdatingLocation()
}
}
// We adopt the CLLocationManagerDelegate
extension ViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
print("THIS IS THE LOCATION \(location.coordinate.latitude)")
}
// This will stop updating the location.
locationManager.stopUpdatingLocation()
}
}
This will get you somewhere ;)
Good luck
Paul M
16,370 PointsDo I need to add something to info.plist? Because this is not working
Paul M
16,370 PointsPaul M
16,370 PointsBut you set the location, I need just the coordinates of the user's current location.