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
  • 9,373
  • 64
  • 206
  • 383

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,142
  • 1
  • 13
  • 34
Khuong
  • 10,526
  • 13
  • 75
  • 108
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,142
  • 1
  • 13
  • 34
Iyyappan Ravi
  • 3,140
  • 1
  • 15
  • 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,238
  • 20
  • 28
Satyanarayana
  • 1,053
  • 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,101
  • 3
  • 13
  • 36
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
  • 3,988
  • 5
  • 30
  • 57
Akhi
  • 657
  • 1
  • 4
  • 14