ios - swift : How to save latitude and longitude to use in another swift file

标签 ios swift maps mkmapview cllocationmanager

我在 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/

相关文章:

ios - 在 XCode 8 StoryBoard 中,View Controller 未与 Container View 一起显示

iphone - 在不同的 UIViewControllers 中重用自定义 UITableView

ios - 使用 objectsWhere() 查询 Realm 数据库时的 CVaListPointer

ios - 无法在新的自定义类 Xcode 11 中连接到 IBOutlet

Swift watchOS 2 - CMSensorDataList

java - 将纬度/经度点转换为墨卡托投影上的像素 (x,y)

maps - 如何在 Elixir 中连接两个 map

ios - 如何在 Swift 中模拟 NSLocale.preferredLanguages 来测试请求?

swift - 如何在 SceneKit 中的 "own"轴上移动旋转的 SCNNode?

python - 用 Folium 显示以米为单位的半径