swift - 需要快速检索用户坐标

标签 swift mapkit

所以我需要将用户的位置坐标存储在数据库中。到目前为止,我已经创建了一个具有数据库的 viewcontroller.swift ,但我将 mapkit 附加到另一个 View Controller ,它在其中打印出坐标。我想知道如何获取这些坐标并将其存储在我的第一个 View Controller 中创建的数据库中。

打印坐标

func locationManager(_ manager: CLLocationManager, didUpdateLocations 
locations: [CLLocation]) {
    if let location = locations.first{
        print(location.coordinate)
    }

此处需要输入

let building = buildingField.text?.trimmingCharacters(in:
.whitespacesAndNewlines)


if(building?.isEmpty)!{
        print("Building is Empty")
    }

if sqlite3_bind_text(stmt, 5, building, -1, nil) != SQLITE_OK{
        print("Error binding building")
    }

最佳答案

您始终可以通过在当前 VC 中创建该类的实例来访问另一个 ViewController 的变量。在这种情况下,您可以创建一个 VC 实例,其中 SQLite DB 代码存在于 MapViewController 中,然后将坐标分配给第一个 VC 中的变量。如果您需要执行诸如通过传递坐标写入数据库之类的任务,那么您可以使用 NSNotificationCenter 类,它允许您在 View Controller 之间进行通信。下面的链接应该能让您更好地了解我在说什么。

引用:

StackOverflow post on passing values

Medium Tutorial on NSNotificationCenter

关于swift - 需要快速检索用户坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50283265/

相关文章:

swift - 在同一 if 语句中解包字符串并检查是否为空

ios - UISplitViewController 中嵌入的 UIViewController 在 iPhone 上不显示栏按钮

ios - NSLayoutAttribute中的Top和TopMargin有什么区别?

ios - MapKit - Swift 示例

iphone - 如何在跟踪用户时将 map View 居中

ios - 如何从 iOS 中的 mapKit 获取纬度、经度?

ios - 如何在 Swift 中使用 ABPeoplePickerNavigationController 选择多个联系人

swift - 在 Swift 解释中遍历字母表

ios - 将MapKit经纬度转为DMS格式

ios - MapKit:如何使用 .xib 文件上的按钮创建自定义注释 View ?