当前位置 swift iOS 9
Current location in swift iOS 9
我在 plist 中添加了 NSLocationWhenInUseUsageDescription 并将其设置为 YES,这是我的代码:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
print(location)
print(center)
}
但是还是不行。它只是不显示任何内容。
看起来 locationManager 函数从未被调用过。它甚至不打印任何记录。
刚刚通过添加解决了它:
locationManager = CLLocationManager()
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
并将 locationManager 声明为 class 变量。 var locationManager:CLLocationManager!
我在 plist 中添加了 NSLocationWhenInUseUsageDescription 并将其设置为 YES,这是我的代码:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
print(location)
print(center)
}
但是还是不行。它只是不显示任何内容。 看起来 locationManager 函数从未被调用过。它甚至不打印任何记录。
刚刚通过添加解决了它:
locationManager = CLLocationManager()
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
并将 locationManager 声明为 class 变量。 var locationManager:CLLocationManager!