我正在使用Apple Watch app来检索用户位置。我已经将CoreLocation框架导入到interfacecontroller类中,并将委托添加到了类中。这是行不通的。请检查以下代码。我错过了什么?
import Foundation
import WatchKit
import CoreLocation
class NearByController: WKInterfaceController, CLLocationManagerDelegate {
override func awakeWithContext(context: AnyObject?) {
var locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}
}
// MARK: CLLocationManagerDelegate
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
println("Retrieved location")
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println(error)
}
答案 0 :(得分:1)
似乎我需要将CLLocationManager初始化为类属性而不是内部方法。这是我的答案。它现在正在运作。
"results" : [
{
"geometry" : {
"location" : {
"lat" : 37.4224764,
"lng" : -122.0842499
},
}