【问题标题】:Show multiple locations on google Maps from Firestore Swift从 Firestore Swift 在谷歌地图上显示多个位置
【发布时间】:2019-06-14 20:08:28
【问题描述】:

我正在尝试在谷歌地图上显示位置,我从 Firestore 获取经度和纬度。

我创建了一个存储经纬度的结构

struct Location {

var latitude: String = ""
var longitute: String = ""
}

这是我获取经度和纬度的 Firestore 代码

for document in snapshot!.documents {
self.location.append(Location(latitude: "\(document.data()["Latitude"] ?? "")", longitute: "\(document.data()["longitude"] ?? "")"))
print(self.location)

    guard let long = document.data()["Latitude"] as? String else { return}
    guard let lat = document.data()["longitude"] as? String else { return}
    let markerStart = GMSMarker(position: CLLocationCoordinate2D(latitude: Double(long) ?? 0.0, longitude: Double(lat) ?? 0.0))
    markerStart.map = self.mapView
}

我在控制台中获取位置,但是当我将其转换为双打试图在谷歌地图上显示它时,它不起作用。请帮忙?

 Document Value is ["userid": 24xDkrtBV6cJrBvRD3U0PmyBF3o2, "createddatetime": FIRTimestamp: seconds=1546584489 nanoseconds=461000000>, "user_role": sales man, "Latitude": 20.6108261, "longitude": 72.9269003, "batterypercentage": 66, "name": Keyur , "company_code": 001]

【问题讨论】:

  • 请打印您的文件价值。
  • @DixitAkabari...编辑了我的问题
  • 您是否在不添加标记的情况下检查地图加载是否正确?
  • 加载正常

标签: ios swift google-maps google-cloud-firestore


【解决方案1】:

所以你有这个数据:

["userid": 24xDkrtBV6cJrBvRD3U0PmyBF3o2, "createddatetime": FIRTimestamp: seconds=1546584489 nanoseconds=461000000>, "user_role": sales man, "Latitude": 20.6108261, "longitude": 72.9269003, "batterypercentage": 66, "name": Keyur , "company_code": 001]

代替这种代码:

for document in snapshot!.documents {
self.location.append(Location(latitude: "\(document.data()["Latitude"] ?? "")", longitute: "\(document.data()["longitude"] ?? "")"))
print(self.location)

    guard let long = document.data()["Latitude"] as? String else { return}
    guard let lat = document.data()["longitude"] as? String else { return}
    let markerStart = GMSMarker(position: CLLocationCoordinate2D(latitude: Double(long) ?? 0.0, longitude: Double(lat) ?? 0.0))
    markerStart.map = self.mapView
}

我们可以这样更好,

    for document in snapshot!.documents {
        self.location.append(Location(latitude: "\(document.data()["Latitude"] ?? "")", longitute: "\(document.data()["longitude"] ?? "")"))
        print(self.location)

        guard let latitude = document.data()["Latitude"] as? Double,
            let longitude = document.data()["Latitude"] as? Double else { return }

        let markerStart = GMSMarker(position: CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        markerStart.map = self.mapView
    }

程序没有到达第 72 73 74 行的原因是因为guard let。它无法从您的document.data() 转换为您假定的Double 纬度和经度String。像我上面的代码那样做,然后你可以根据需要进一步改进它。

【讨论】:

  • 实际上它不会在打印语句后调用我的代码:(
  • 你只需要拖出(或点击禁用)你的断点(那个蓝色箭头)
  • 好的,让我试试看,先生 :)
  • @wings 不需要“先生”。再次阅读我的最新编辑,解释为什么程序没有到达第 72、73 和 74 行,就像你提到的那样。这应该可以解决您的问题。
  • 先生,它给了我两个错误,第一个是Variable used within its own initial value,还有一个要问这个Expected ',' separator
猜你喜欢
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多