use*_*888 14 core-location cllocationmanager ios swift
我在我的视图控制器上有这个代码,但这不起作用:
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"
}
Run Code Online (Sandbox Code Playgroud)
我对其他帖子中的阅读方式有所了解.但我没有得到永远,没有印刷品..
谢谢!!
jay*_*iya 19
首先在plist文件中添加这两行
1) NSLocationWhenInUseUsageDescription
2) NSLocationAlwaysUsageDescription
然后这是班级工作完成这个
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)")
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是在Swift 3中获取用户位置的简单步骤
1)首先在带有描述的plist文件中添加此行
NSLocationWhenInUseUsageDescription
Run Code Online (Sandbox Code Playgroud)
2)在项目中添加CoreLocation.framework(在Build Phases-> Link Binary with Library部分下)
3)在AppDelegate类中
import CoreLocation
Run Code Online (Sandbox Code Playgroud)
4)按如下方式创建locationManager对象
var locationManager:CLLocationManager!
Run Code Online (Sandbox Code Playgroud)
5)在didFinishLaunchingWithOptions中写下面的代码
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 200
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
Run Code Online (Sandbox Code Playgroud)
6)确认CLLocationManagerDelegate委托如下
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate
Run Code Online (Sandbox Code Playgroud)
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)")
}
Run Code Online (Sandbox Code Playgroud)
小智 0
您需要具有初始化函数。
覆盖 init(coder:) 和 init(nibName: bundle:) 并添加您想要的任何自定义 init。
因为您已经说过位置不是可选的,所以您必须在超级 init 调用所有 init 函数之前初始化它。
func init() {
...
location = CLLocationManager()
// either set delegate and other stuff here or in viewDidLoad
super.init(nibName:nil, bundle:nil)
// other initialization below
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39829 次 |
| 最近记录: |