Swift Playground中的CoreLocation

时间:2017-03-30 18:16:50

标签: swift xcode location core-location swift-playground

我正在尝试检索用户的当前位置,但是在Swift Playgrounds中。我已经有了这个代码但由于某种原因它从不执行print语句。有人可以帮忙吗?

import UIKit  
import CoreLocation  
import PlaygroundSupport  

var str = "Hello, playground"  
let locationManager = CLLocationManager()  
PlaygroundPage.current.needsIndefiniteExecution = true  

class GeoCoordinateDelegate: NSObject, CLLocationManagerDelegate {  

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {  
        let location = locations[0]  
        print("Most Recent Location: " + location.description)  
        print(location.coordinate.latitude)  
        print(location.coordinate.longitude)  
    }  

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {  
        print("Error while updating " + error.localizedDescription)  
    }  

}  
let geoCoordinateDelegate = GeoCoordinateDelegate()  
locationManager.delegate = geoCoordinateDelegate  
locationManager.desiredAccuracy = kCLLocationAccuracyBest  
locationManager.startUpdatingLocation()  

1 个答案:

答案 0 :(得分:1)

编写CoreLocation代码时,最好在尝试执行任何与位置相关的操作之前检查CLLocationManager.locationServicesEnabled()

在Swift Playgrounds(2017-03-21 1.2)中,该方法返回false,因为CoreLocation不适用于游乐场(但是?)。我建议在https://bugreport.apple.com/

提交错误报告