我正在将我的应用程序更新到 Swift 3,但无法让它打印出位置坐标。
我的 info.plist 文件中有“隐私 - 使用时位置”、“隐私 - 始终位置”和“隐私 - 位置使用说明”。
@IBAction func activateTestModeButton(_ sender: AnyObject) {
locationManager.startUpdatingLocation()
print("Method called")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations newLocation: CLLocation, fromLocation oldLocation: CLLocation){
loadUser()
loadCoreData()
print("Updating location")
let latitude:String = "\(newLocation.coordinate.latitude)"
let longitude:String = "\(newLocation.coordinate.longitude)"
print("Latitude = \(newLocation.coordinate.latitude)")
print("Longitude = \(newLocation.coordinate.longitude)") }
它打印“调用的方法”,但没有其他内容。我还包含了 didFailWithError 方法,我认为这是必需的,它不会打印任何内容:
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error){
print("Location manager failed with error = \(error)")
}
人们认为我拥有的其他相关代码:
/* Asking the user if we can access their location */
func Authorization(){
if CLLocationManager.locationServicesEnabled()
{
switch CLLocationManager.authorizationStatus()
{
case .authorizedAlways:
print("Authorized")
case .authorizedWhenInUse:
print("Authorized when in use")
case .denied:
displayAlertWithTitle("Not determined", message: "Location services are not allowed for this app")
case .notDetermined:
print("Not Determined")
if let manager = self.locationManager
{
manager.requestWhenInUseAuthorization()
}
case .restricted:
displayAlertWithTitle("Restricted", message: "Location services are not allowed for this app")
}
}
else
{
print("Location services are not enabled")
}
}
/* Location Detection */
func permission(){
if CLLocationManager.authorizationStatus() == .notDetermined
{
locationManager?.requestAlwaysAuthorization()
}
}
override func viewDidAppear(_ animated: Bool){
super.viewDidAppear(animated)
Authorization()
}
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager = CLLocationManager()
self.locationManager?.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager?.distanceFilter = 200
self.locationManager?.delegate = self
}
最佳答案
我建议您将管理 Location 的代码移动到像这样的类中。这段代码来 self 几个月前制作的一个应用程序,我测试了它是否有效,我确实如此。所以试试吧。您只需要创建此类的对象。我在方法 locationManager(:didupdate)
中留下了注释打印,您可以取消注释以查看您所在位置的所有更改。
我知道它并不完美,但它让我完成了工作。
import Foundation
import MapKit
import CoreLocation
class Location: CLLocationManager, CLLocationManagerDelegate {
private var latitude:Double
private var longitud:Double
private let locationManager = CLLocationManager()
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
override init() {
self.latitude = 0.0
self.longitud = 0.0
self.locationManager.requestWhenInUseAuthorization()
super.init()
if CLLocationManager.locationServicesEnabled() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
internal func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = (manager.location?.coordinate)!
//print("locations = \(locValue.latitude) \(locValue.longitude)")
self.latitude = locValue.latitude
self.longitud = locValue.longitude
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
func getLatitude()->NSNumber{
return self.latitude as NSNumber
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
func getLongitude()->NSNumber{
return self.longitud as NSNumber
}
}
关于ios - didUpdateLocations 在 Swift 3 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44291337/