Swift游乐场的核心定位
我正在尝试检索用户的当前位置,但在Swift操场中。我已经有了这段代码,但出于某种原因,它从未执行print语句。有人能帮忙吗Swift游乐场的核心定位,swift,xcode,location,core-location,swift-playground,Swift,Xcode,Location,Core Location,Swift Playground,我正在尝试检索用户的当前位置,但在Swift操场中。我已经有了这段代码,但出于某种原因,它从未执行print语句。有人能帮忙吗 import UIKit import CoreLocation import PlaygroundSupport var str = "Hello, playground" let locationManager = CLLocationManager() PlaygroundPage.current.needsIndefiniteExecuti
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()
编写CoreLocation代码时,最好先检查
CLLocationManager.locationServicesEnabled()
,然后再尝试执行任何与位置相关的操作
在Swift游乐场(2017-03-21的1.2版)中,该方法返回false
,因为核心位置不适用于游乐场(尚未?)。我建议在