【问题标题】:Swift - Retrieving Geopoints from Firestore, how to show them as map annotationsSwift - 从 Firestore 检索地理点,如何将它们显示为地图注释
【发布时间】:2019-02-21 18:23:34
【问题描述】:

我有以下代码可以检索我存储在 Google Firestore 中的坐标:

    let locationsRef = db.collection("locatioInfo")
    locationsRef.getDocuments { (querySnapshot, err) in
        if let err = err {
            print("Error receiving Firestore snapshot: \(err)")
        } else {
            for document in querySnapshot!.documents {
                print("\(document.documentID) => \(document.data())")
            }
        }
    }

产生这个输出:

location1 => ["geopoint": <FIRGeoPoint: (50.086421, 14.452333)>]
location2 => ["geopoint": <FIRGeoPoint: (50.086442, 14.452268)>]

我的问题是:如何将获取的 FIRGeoPoints 转换为 CLLocationCoordinates2D 或者简单地说:如何使用这些坐标并在地图上显示它们?

我有一个 MKAnnotationView 已经设置的广告用于硬编码坐标,现在我需要显示获取的坐标。希望这已经足够清楚了,在此先感谢。

【问题讨论】:

    标签: ios swift firebase google-cloud-firestore mapkit


    【解决方案1】:

    我认为您要问的是如何从 Firestore 地理点 (??) 导出纬度和经度。试试这个,如果不是你要的,请告诉我,我会修改。

    暂时假设您有这样的 Firestore 结构

    coordinates
       point_0
           coords: [49N, 44E]
       point_1
           coords: [67N, 30E]
    

    然后阅读这些内容并获取纬度和经度。

    self.db.collection("coordinates").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                if let coords = document.get("coords") {
                    let point = coords as! GeoPoint
                    let lat = point.latitude
                    let lon = point.longitude
                    print(lat, lon) //here you can let coor = CLLocation(latitude: longitude:)
                }
            }
        }
    }
    

    输出是

    49.0 44.0
    67.0 30.0
    

    【讨论】:

    • 谢谢,这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多