在Info.plist文件中添加如下配置:
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription
swift其实做法类似objectc:
locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=
10
;
if
(iOSVersion>=
8
) {
[locationManager requestWhenInUseAuthorization];
//使用程序其间允许访问位置数据(iOS8定位需要)
}
[locationManager startUpdatingLocation];
//开启定位
下面自己写的swift调用
import CoreLocation
class xxxxx: UIViewController,CLLocationManagerDelegate{
class xxxxx: UIViewController,CLLocationManagerDelegate{
override func viewDidLoad()
{
super.viewDidLoad()
m_lm = CLLocationManager()
m_lm.delegate = self
m_lm.desiredAccuracy = kCLLocationAccuracyBest
m_lm.distanceFilter = kCLLocationAccuracyKilometer
if #available(iOS 8.0, *) {
m_lm.requestAlwaysAuthorization()
m_lm.requestWhenInUseAuthorization()
}
m_lm.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
}
print(locations)
}
}
别人的代码:
import UIKit import CoreLocation class ViewController: UIViewController , CLLocationManagerDelegate { var locationManager : CLLocationManager! var seenError : Bool = false var locationFixAchieved : Bool = false var locationStatus : NSString = "not Started" var info : UILabel? var longitudeLabel_int : UILabel? //the longitude before the degree sign (integer section) var longitudeLabel_dec : UILabel? //the longitude after the degree sign (decimal section) var latitudeLabel_int : UILabel? //the latitude before the degree sign (integer section) var latitudeLabel_dec : UILabel? //the latitude after the degree sign (decimal section) var O_longitude : UILabel? //the degree sign var O_latitude : UILabel? //the degree sign func initLocationManager() { seenError = false locationFixAchieved = false locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.distanceFilter = kCLLocationAccuracyKilometer locationManager.requestAlwaysAuthorization() } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let buttomEdge = UILabel(frame: CGRect(x: 0, y: 569, width: 320, height: 0.2)) buttomEdge.backgroundColor = UIColor.redColor() let signature = UILabel(frame: CGRect(x: 160, y: 539, width: 160, height: 30)) signature.text = "孙毅 April 25th 2015" signature.textColor = UIColor.whiteColor() info = UILabel(frame: CGRect(x: 20, y: 20, width: 300, height: 50)) info!.textAlignment = NSTextAlignment.Center info!.textColor = UIColor.whiteColor() longitudeLabel_int = UILabel(frame: CGRect(x: 88, y: 150, width: 30, height: 50)) longitudeLabel_int!.textAlignment = NSTextAlignment.Right longitudeLabel_int!.text = "000" longitudeLabel_int!.font = UIFont(name: "Times New Roman", size: 20) longitudeLabel_int!.textColor = UIColor.blueColor() O_longitude = UILabel(frame: CGRect(x: 120, y: 145, width: 5, height: 50)) O_longitude!.textAlignment = NSTextAlignment.Left O_longitude!.text = "o" O_longitude!.font = UIFont(name: "Arial", size: 10) O_longitude!.textColor = UIColor.blueColor() longitudeLabel_dec = UILabel(frame: CGRect(x: 130, y: 150, width: 200, height: 50)) longitudeLabel_dec!.textAlignment = NSTextAlignment.Left longitudeLabel_dec!.text = "000000" longitudeLabel_dec!.font = UIFont(name: "Times New Roman", size: 20) longitudeLabel_dec!.textColor = UIColor.blueColor() latitudeLabel_int = UILabel(frame: CGRect(x: 88, y: 200, width: 30, height: 50)) latitudeLabel_int!.textAlignment = NSTextAlignment.Right latitudeLabel_int!.text = "000" latitudeLabel_int!.font = UIFont(name: "Times New Roman", size: 20) latitudeLabel_int!.textColor = UIColor.redColor() O_latitude = UILabel(frame: CGRect(x: 120, y: 195, width: 5, height: 50)) O_latitude!.textAlignment = NSTextAlignment.Left O_latitude!.text = "o" O_latitude!.font = UIFont(name: "Arial", size: 10) O_latitude!.textColor = UIColor.redColor() latitudeLabel_dec = UILabel(frame: CGRect(x: 130, y: 200, width: 200, height: 50)) latitudeLabel_dec!.textAlignment = NSTextAlignment.Left latitudeLabel_dec!.text = "000000" latitudeLabel_dec!.font = UIFont(name: "Times New Roman", size: 20) latitudeLabel_dec!.textColor = UIColor.redColor() self.initLocationManager() self.view.addSubview(info!) self.view.addSubview(longitudeLabel_int!) self.view.addSubview(O_longitude!) self.view.addSubview(longitudeLabel_dec!) self.view.addSubview(latitudeLabel_int!) self.view.addSubview(O_latitude!) self.view.addSubview(latitudeLabel_dec!) self.view.addSubview(buttomEdge) self.view.addSubview(signature) } func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) { locationManager.stopUpdatingLocation() if (nil != error) { if (false == seenError) { seenError = true info!.text = "Error" } } } func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { if (false == locationFixAchieved) { locationFixAchieved = true var locationArray = locations as NSArray var locationObj = locationArray.lastObject as! CLLocation var coordinate = locationObj.coordinate var degree : Int = 0 var minute : Int = 0 var second : Double = 0.0 degree = Int(coordinate.longitude) minute = Int((coordinate.longitude - Double(degree)) * 60) second = (coordinate.longitude - Double(degree) - Double(minute)/60.0) * 3600 var second_4 = NSString(format: "%.4f", second) //show only 4 decimal places if (degree >= 0) { longitudeLabel_int!.text = "\(degree)" longitudeLabel_dec!.text = "\(minute)\' \(second_4.doubleValue)" E" } else { longitudeLabel_int!.text = "\(-degree)" longitudeLabel_dec!.text = "\(-minute)\' \(-second_4.doubleValue)" W" } degree = Int(coordinate.latitude) minute = Int((coordinate.latitude - Double(degree)) * 60) second = (coordinate.latitude - Double(degree) - Double(minute)/60.0) * 3600 second_4 = NSString(format: "%.4f", second) if (degree >= 0) { latitudeLabel_int!.text = "\(degree)" latitudeLabel_dec!.text = "\(minute)\' \(second_4.doubleValue)" N" } else { latitudeLabel_int!.text = "\(-degree)" latitudeLabel_dec!.text = "\(-minute)\' \(-second_4.doubleValue)" S" } } } 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 (true == shouldIAllow) { info!.text = "Localization is allowed" locationManager.startUpdatingLocation() } else { info!.text = "Denied access: \(locationStatus)" } } override func didReceiveMemoryWarning() {