1

I am trying to get the long and lat of my users locations, I have done the following:

imported Core Location

import CoreLocation

Add Core Location Delegate

class ViewController: UIViewController, CLLocationManagerDelegate, AVCaptureMetadataOutputObjectsDelegate, DTDeviceDelegate {

defined this variable:

var locationManager: CLLocationManager!

and then added this method:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0] 
        let long = userLocation.coordinate.longitude;
        let lat = userLocation.coordinate.latitude;

        print(long, lat)

        //Do What ever you want with it
    }

but the location does not get printed, my method does not even get it.

I add the items to use Core Location to my plist and the app ask me to use the location services when I first run it. But now location is getting printed....what am I doing wrong?

user979331
  • 11,039
  • 73
  • 223
  • 418

5 Answers5

5

Maybe you forgot to set value for the delegate

In your viewDidLoad() add this:

if CLLocationManager.locationServicesEnabled() {
    locationManager.requestAlwaysAuthorization()
    locationManager.requestWhenInUseAuthorization()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
    mapView.showsUserLocation = true
}
regetskcob
  • 1,172
  • 1
  • 13
  • 35
Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116
2

Use this Code,

declare locationManager

var locationManager = CLLocationManager()

Set the Delegate in viewDidLoad, shown below code,

override func viewDidLoad() {

    super.viewDidLoad()

    locationManager.delegate = self

    if CLLocationManager.authorizationStatus() == .NotDetermined {
        self. locationManager.requestWhenInUseAuthorization()
    }

    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
}
regetskcob
  • 1,172
  • 1
  • 13
  • 35
Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30
1

You neeed to initialize the locationManageger and enable the location flag in xcode also, it appaear bottom of the xcode

self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

if #available(iOS 8.0, *) {

    if  Bundle.main.infoDictionary?["NSLocationAlwaysUsageDescription"] != nil {

        self.locationManager.requestAlwaysAuthorization()
    } 
    else {

        self.locationManager.requestWhenInUseAuthorization()
    }    
}

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.startUpdatingLocation()
ZGski
  • 2,398
  • 1
  • 21
  • 34
Satyanarayana
  • 1,059
  • 6
  • 16
1

Add this in viewdidload,

if (CLLocationManager.locationServicesEnabled())
{
     locationManager.delegate = self
     locationManager.desiredAccuracy = kCLLocationAccuracyBest
     if ((UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8)
     {
          locationManager.requestWhenInUseAuthorization()
     }

     locationManager.startUpdatingLocation()
}
else
{
     #if debug
     println("Location services are not enabled");
     #endif           
}

Then add this two delegate method to get location,

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
{
    locationManager.stopUpdatingLocation()
    if ((error) != nil)
    {
        print(error)
    }
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
{
    var locationArray = locations as NSArray
    var locationObj = locationArray.lastObject as! CLLocation
    var coord = locationObj.coordinate
    print(coord.latitude)
    print(coord.longitude)

}

This way you will get your current location.

Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38
0

You can add this code to your viewDidLoad() method:

locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()

This will call the locationManagers didUpdate delegate-methods if location services are available for your app

Ottavio Campana
  • 4,088
  • 5
  • 31
  • 58
Akhilesh Mourya
  • 666
  • 1
  • 4
  • 15