我在 ViewController 中有这段代码来捕获用户的当前位置:
@IBOutlet weak var userMapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location : CLLocationCoordinate2D = manager.location!.coordinate
print(location.latitude)
let latitudeDelta : CLLocationDegrees = 0.01
let longitudeDelta : CLLocationDegrees = 0.01
let span : MKCoordinateSpan = MKCoordinateSpanMake(latitudeDelta, longitudeDelta)
let center : CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.latitude, location.longitude)
let region : MKCoordinateRegion = MKCoordinateRegionMake(center, span)
self.userMapView.setRegion(region, animated: false)
self.userMapView.removeAnnotations(userMapView.annotations)
let userPinLocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.latitude, location.longitude)
let pinAnnotation = MKPointAnnotation()
pinAnnotation.coordinate = userPinLocation
pinAnnotation.title = "Sua localização atual"
self.userMapView.addAnnotation(pinAnnotation)
}
我有另一个带有数据库函数的 swift 文件,我需要保存纬度和经度以在数据库中使用
如何将纬度和经度存储在变量中以便在 locationManager 函数外部使用?
最佳答案
在 locationManager(_:didUpdateLocations:) 实例方法中执行数据库操作。 或者 var 全局模型。
关于ios - swift : How to save latitude and longitude to use in another swift file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39431945/