0

I'm trying to get the users coordinates, but I cant work out how to retrieve them? Here is my code for finding their location on a map, what should I add to get numerical values for longitude/latitude? It works fine, and I've added the requestAuthorization key into info.plist.I've tried using the CLLocation object, but I cant seem to get it to work.

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var Map: MKMapView!


var locationMgr = CLLocationManager()


override func viewDidLoad() {
    super.viewDidLoad()


    locationMgr.delegate = self
    locationMgr = CLLocationManager()
    locationMgr.requestAlwaysAuthorization()
     locationMgr.startUpdatingLocation()
            Map.showsUserLocation = true


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}
mehdi lotfi
  • 11,194
  • 18
  • 82
  • 128
Learnin
  • 1,221
  • 4
  • 15
  • 19
  • You have to implement this method to actually retrieve the location: `- func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[])` – Lyndsey Scott Aug 03 '14 at 13:18
  • Thanks for the quick response! Any chance you could throw me some sample code where it prints the location to console? println(locations) within the func doesnt seem to work, I think its constantly returning nil values? I'm really new to this, its my first week coding! – Learnin Aug 03 '14 at 13:47
  • http://stackoverflow.com/questions/24063798/cllocation-manager-in-swift-to-get-location-of-user –  Aug 03 '14 at 14:07
  • Thanks! Helpful link :) I've got it working now, and in from the view controller as well – Learnin Aug 04 '14 at 12:53

2 Answers2

0

Implement the CLLocationManager delegate protocol methods, specifically:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
Joride
  • 3,722
  • 18
  • 22
  • Hey! Same as I just said to Lyndsey, Any chance you could throw me some sample code where it prints the location to console? println(locations) within the func doesnt seem to work, I think its constantly returning nil values? – Learnin Aug 03 '14 at 13:48
  • If this is your first week of coding, use the introductory material from Apple for Objective-C first (https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/index.html#//apple_ref/doc/uid/TP40011343 and https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/index.html#//apple_ref/doc/uid/TP40011343). Swift is still in beta, so I would not recommend using Swift to learn programming. Anyway, to print out the locations in Swift use: println(\\(locations)) – Joride Aug 03 '14 at 15:58
  • I'm learning it as I'm enjoying the tangible results of x code, and swift is just that much more intuitive than obj C. Got it working now :) – Learnin Aug 04 '14 at 12:55
  • Swift is no longer in beta, and is more than stable enough to learn coding with at this point. – Unome Sep 09 '15 at 05:09
0

You've initialised the location manager twice. so the delegate you set after first time the manager initialised is nil. so delete this locationMgr = CLLocationManager() in your viewDidLoad method. it should work