[英]When using location manager, in xcode/swift, to get a user's current location, I get a value of nil. Any suggestions?
*更新:現在獲取位置數據,打印語句print("\(currentLocation.coordinate.latitude)")
有效。 如果我嘗試將 = 右側的內容分配給 label.text 值,則會出現錯誤:在展開可選值時意外發現 nil。 *對整個 Swift/Xcode 來說還是個新手。 任何幫助將不勝感激。 我正在運行以下代碼(僅與所示問題相關的代碼)。 當用戶按下按鈕並選擇獲取位置時,它應該獲取他們的位置。 相反,我什么也得不到。 locationManager 中的文本從不打印,我很茫然。
import UIKit
import MapKit
import CoreLocation
import MessageUI
import Photos
import AVFoundation
class ViewController: UIViewController, UITextViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, CLLocationManagerDelegate, MFMailComposeViewControllerDelegate {
var locationManager: CLLocationManager()
var currentLocation: CLLocation?
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let currentLocation = locations[0]
print("\(currentLocation.coordinate.latitude)")
issueLocation.text = "\(currentLocation.coordinate.latitude)" ***ERROR***
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error: \(error)")
}
沒關系的東西,我希望,還有更多代碼
// Attach a location
@IBAction func attachLocation(_ sender: UIButton) {
if issueLocation.text == nil {
issueImage.isHidden = true
}
let alertController = UIAlertController(title: "Location options", message: nil, preferredStyle: .actionSheet)
// Get location
let locationAction = UIAlertAction(title: "Get location", style: .default) { (action) in
self.displayLocation()
}
// Add get location action to alert controller
alertController.addAction(locationAction)
// Remove location
let eraseLocation = UIAlertAction(title: "Erase location", style: .default) { (action) in
self.issueLocation.text == nil
self.issueLocation.isHidden = true
}
// Add erase location to alert controller
alertController.addAction(eraseLocation)
// Cancel action
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
// Do nothing
}
// Add cancel action to alert controller
alertController.addAction(cancelAction)
// Display alert message on screen
self.present(alertController, animated: true) {
// Code to handle user selection
}
}
func displayLocation() {
let status = CLLocationManager.authorizationStatus()
let noPermissionMessage = "It appears that LoCAL Information does not have access to your location. Click Settings -> LoCAL Information -> Location to allow access to your location."
switch status {
case .notDetermined:
locationManager?.requestWhenInUseAuthorization()
case .authorized, .authorizedAlways, .authorizedWhenInUse:
print("JJJJJ")
print("KKKKK")
issueLocation.isHidden = false
case .denied, .restricted:
self.troubleAlert(message: noPermissionMessage)
@unknown default:
self.troubleAlert(message: noPermissionMessage)
}
}
locationManager
已聲明但未初始化
代替
var locationManager: CLLocationManager?
和
let locationManager = CLLocationManager()
並在每次出現locationManager
后刪除問號
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.