I am trying to make a simple app that shows: Latitude Longitude Horizontal Accuracy Altitude Vertical Accuracy Location From Start
我正在尝试制作一个简单的应用程序,显示:纬度经度水平精度海拔垂直精度位置从开始
My code is:
我的代码是:
@IBOutlet weak var latitude: UILabel!
@IBOutlet weak var longitude: UILabel!
@IBOutlet weak var horizontalAccuracy: UILabel!
@IBOutlet weak var altitude: UILabel!
@IBOutlet weak var verticalAccuracy: UILabel!
@IBOutlet weak var distance: UILabel!
var locationManager: CLLocationManager = CLLocationManager()
var startLocation: CLLocation!
@IBAction func resetDistance(sender: AnyObject) {
startLocation = nil
}
func locationManager(manager: CLLocationManager!,
didUpdateLocations locations: [AnyObject]!)
{
var latestLocation: AnyObject = locations[locations.count - 1]
latitude.text = String(format: "%.4f",
latestLocation.coordinate.latitude)
longitude.text = String(format: "%.4f",
latestLocation.coordinate.longitude)
horizontalAccuracy.text = String(format: "%.4f",
latestLocation.horizontalAccuracy)
altitude.text = String(format: "%.4f",
latestLocation.altitude)
verticalAccuracy.text = String(format: "%.4f",
latestLocation.verticalAccuracy)
if startLocation == nil {
startLocation = latestLocation as! CLLocation
}
var distanceBetween: CLLocationDistance =
latestLocation.distanceFromLocation(startLocation)
distance.text = String(format: "%.2f", distanceBetween)
}
func locationManager(manager: CLLocationManager!,
didFailWithError error: NSError!) {
}
And then in the ViewDidLoad section:
然后在ViewDidLoad部分:
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
startLocation = nil
I keep getting the error: on the latitude.text line (everything hooked up correctly)
我一直收到错误:在latitude.text行(所有内容都正确连接)
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
How would I go about adding if let statements? Can you help me find the problem?
如果让let语句,我该如何添加呢?你能帮我找到问题吗?
Thanks!
2 个解决方案
#1
Welcome to Swift. You can't do anything with an AnyObject. Cast down like this:
欢迎来到Swift。你不能对AnyObject做任何事情。像这样抛弃:
let latestLocation = locations.last as! CLLocation
Also, you say "everything hooked up correctly", but I'm not sure I believe that; try doing println(latitude)
to make sure.
此外,你说“一切都正确连接”,但我不确定我是否相信;尝试做println(纬度)以确保。
Finally, this code is not going to work:
最后,这段代码不起作用:
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
The problem is that the authorization request is asynchronous. Thus you possibly are starting to update locations before you've actually got authorization. You have to wait until you actually have authorization before you start asking for updates.
问题是授权请求是异步的。因此,您可能在获得授权之前开始更新位置。在开始要求更新之前,您必须等到实际获得授权。
#2
About how to get authorizaiton:
关于如何获得授权:
First add NSLocationWhenInUseUsageDescription in to your info.plist file
首先将NSLocationWhenInUseUsageDescription添加到info.plist文件中
Then in viewDidload:
然后在viewDidload中:
if NSString(string:UIDevice.currentDevice().systemVersion).doubleValue > 8 {
locationManager.requestWhenInUseAuthorization()
}
Then is this delegate method,start updateingLoation
然后是这个委托方法,启动updateingLoation
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status != CLAuthorizationStatus.Denied{
locationManager.startUpdatingLocation()
}
}
Then when you get location,you can convert it to CLLocation
as matt post
然后,当您获得位置时,您可以将其转换为CLLocation作为亚光贴
You may also refer to this link about details, this is an answer I answered before Swift: Exception while trying to print CLLocationSpeed "unexpectedly found nil while unwrapping an Optional value"
你也可以参考这个关于细节的链接,这是我在Swift之前回答的一个答案:尝试打印CLLocationSpeed时出现异常“在解开一个Optional值时意外发现了nil”
#1
Welcome to Swift. You can't do anything with an AnyObject. Cast down like this:
欢迎来到Swift。你不能对AnyObject做任何事情。像这样抛弃:
let latestLocation = locations.last as! CLLocation
Also, you say "everything hooked up correctly", but I'm not sure I believe that; try doing println(latitude)
to make sure.
此外,你说“一切都正确连接”,但我不确定我是否相信;尝试做println(纬度)以确保。
Finally, this code is not going to work:
最后,这段代码不起作用:
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
The problem is that the authorization request is asynchronous. Thus you possibly are starting to update locations before you've actually got authorization. You have to wait until you actually have authorization before you start asking for updates.
问题是授权请求是异步的。因此,您可能在获得授权之前开始更新位置。在开始要求更新之前,您必须等到实际获得授权。
#2
About how to get authorizaiton:
关于如何获得授权:
First add NSLocationWhenInUseUsageDescription in to your info.plist file
首先将NSLocationWhenInUseUsageDescription添加到info.plist文件中
Then in viewDidload:
然后在viewDidload中:
if NSString(string:UIDevice.currentDevice().systemVersion).doubleValue > 8 {
locationManager.requestWhenInUseAuthorization()
}
Then is this delegate method,start updateingLoation
然后是这个委托方法,启动updateingLoation
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status != CLAuthorizationStatus.Denied{
locationManager.startUpdatingLocation()
}
}
Then when you get location,you can convert it to CLLocation
as matt post
然后,当您获得位置时,您可以将其转换为CLLocation作为亚光贴
You may also refer to this link about details, this is an answer I answered before Swift: Exception while trying to print CLLocationSpeed "unexpectedly found nil while unwrapping an Optional value"
你也可以参考这个关于细节的链接,这是我在Swift之前回答的一个答案:尝试打印CLLocationSpeed时出现异常“在解开一个Optional值时意外发现了nil”