如何获得位置用户的CLLocationManager在迅速?
我有我的视图控制器上的这段代码,但这不工作:
import UIKit import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate { var location: CLLocationManager! override func viewDidLoad() { super.viewDidLoad() location=CLLocationManager() location.delegate = self location.desiredAccuracy=kCLLocationAccuracyBest location.startUpdatingLocation() } func locationManager(location:CLLocationManager, didUpdateLocations locations:AnyObject[]) { println("locations = \(locations)") label1.text = "success" }
我有我阅读其他职位的权限。 但我从来没有获得,没有println ..
谢谢!!
首先在plist文件中添加这两行
1) NSLocationWhenInUseUsageDescription
2) NSLocationAlwaysUsageDescription
然后这是class级工作完成的工具
import UIKit import CoreLocation @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate { var window: UIWindow? var locationManager: CLLocationManager! var seenError : Bool = false var locationFixAchieved : Bool = false var locationStatus : NSString = "Not Started" func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { initLocationManager(); return true } // Location Manager helper stuff func initLocationManager() { seenError = false locationFixAchieved = false locationManager = CLLocationManager() locationManager.delegate = self locationManager.locationServicesEnabled locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestAlwaysAuthorization() } // Location Manager Delegate stuff func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) { locationManager.stopUpdatingLocation() if (error) { if (seenError == false) { seenError = true print(error) } } } func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) { if (locationFixAchieved == false) { locationFixAchieved = true var locationArray = locations as NSArray var locationObj = locationArray.lastObject as CLLocation var coord = locationObj.coordinate println(coord.latitude) println(coord.longitude) } } func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { var shouldIAllow = false switch status { case CLAuthorizationStatus.Restricted: locationStatus = "Restricted Access to location" case CLAuthorizationStatus.Denied: locationStatus = "User denied access to location" case CLAuthorizationStatus.NotDetermined: locationStatus = "Status not determined" default: locationStatus = "Allowed to location Access" shouldIAllow = true } NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil) if (shouldIAllow == true) { NSLog("Location to Allowed") // Start location services locationManager.startUpdatingLocation() } else { NSLog("Denied access: \(locationStatus)") } } }
以下是在Swift 3中获取用户位置的简单步骤
1)首先在plist文件中join这一行并加以说明
NSLocationWhenInUseUsageDescription
2)在您的项目中添加CoreLocation.framework(在“构build阶段 – >链接二进制库”部分下)
3)在AppDelegate类
import CoreLocation
4)创buildlocationManager对象如下
var locationManager:CLLocationManager!
5)在didFinishLaunchingWithOptions中input以下代码
locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.distanceFilter = 200 locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation()
6)确认CLLocationManagerDelegate代表如下
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate
7)编写CLLocationManagerDelegate委托方法获取用户位置
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print("location error is = \(error.localizedDescription)") } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let locValue:CLLocationCoordinate2D = (manager.location?.coordinate)! print("Current Locations = \(locValue.latitude) \(locValue.longitude)") }
由于你声明的位置是一个显式解包的可选项(CLLocationManager!),所以它需要一个初始化器,或者在jhurraybuild议的init方法中,或者只是内联,如下所示:
var location: CLLocationManager! = nil
请注意,您还有其他可能的问题,包括iOS 8对查询用户是否有权使用CoreLocation有新的要求。 看到这个问题的更多信息。
这和上面的代码是一样的,但是在本贴子发布的date之前,这些代码已经被清理到了Swift中。 这对我有效。
荣誉原来的海报。
(注意,坚持这个到你将用来处理你的位置的东西。)
var lastLocation = CLLocation() var locationAuthorizationStatus:CLAuthorizationStatus! var window: UIWindow? var locationManager: CLLocationManager! var seenError : Bool = false var locationFixAchieved : Bool = false var locationStatus : NSString = "Not Started" override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) self.initLocationManager() } // Location Manager helper stuff func initLocationManager() { seenError = false locationFixAchieved = false locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestAlwaysAuthorization() } // Location Manager Delegate stuff func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) { locationManager.stopUpdatingLocation() if ((error) != nil) { if (seenError == false) { seenError = true print(error) } } } func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { if (locationFixAchieved == false) { locationFixAchieved = true var locationArray = locations as NSArray var locationObj = locationArray.lastObject as CLLocation var coord = locationObj.coordinate println(coord.latitude) println(coord.longitude) } } func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { var shouldIAllow = false switch status { case CLAuthorizationStatus.Restricted: locationStatus = "Restricted Access to location" case CLAuthorizationStatus.Denied: locationStatus = "User denied access to location" case CLAuthorizationStatus.NotDetermined: locationStatus = "Status not determined" default: locationStatus = "Allowed to location Access" shouldIAllow = true } NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil) if (shouldIAllow == true) { NSLog("Location to Allowed") // Start location services locationManager.startUpdatingLocation() } else { NSLog("Denied access: \(locationStatus)") } }
你需要有init函数。
重写init(coder :)和init(nibName:bundle :)并添加你想要的任何自定义的init。
因为你所说的位置不是可选的,所以你必须在你的超级初始化调用你的所有初始化函数之前初始化它。
func init() { ... location = CLLocationManager() // either set delegate and other stuff here or in viewDidLoad super.init(nibName:nil, bundle:nil) // other initialization below }
它应该被写为func locationManager(manager:CLLocationManager,didUpdateLocations locations:[CLLocation]){
做viewcontroller下面的东西[使用swift] –
class ViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate { var locationManager: CLLocationManager? var usersCurrentLocation:CLLocationCoordinate2D? override func viewDidLoad() { super.viewDidLoad() self.locationManager = CLLocationManager() if CLLocationManager.authorizationStatus() == .NotDetermined{ locationManager?.requestAlwaysAuthorization() } locationManager?.desiredAccuracy = kCLLocationAccuracyBest locationManager?.distanceFilter = 200 locationManager?.delegate = self startUpdatingLocation() usersCurrentLocation = CLLocationCoordinate2DMake(LATTITUDE, LONGITUDE) let span = MKCoordinateSpanMake(0.005, 0.005) let region = MKCoordinateRegionMake(usersCurrentLocation!, span) mapview.setRegion(region, animated: true) mapview.delegate = self mapview.showsUserLocation = true }
// MARK:CLLocationManagerDelegate方法
func startUpdatingLocation() { self.locationManager?.startUpdatingLocation() } func stopUpdatingLocation() { self.locationManager?.stopUpdatingLocation() }
// MARK:MKMapViewDelegate
func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation){ mapview.centerCoordinate = userLocation.location!.coordinate mapview.showsUserLocation = true regionWithGeofencing() }