在 swift 中将地图图钉从 firebase 加载到 mapkit,以便所有用户都能看到

Loading map pins from firebase onto mapkit in swift for all users to be able to see

我正在创建一个标记应用程序,用户可以在其中将街头艺术标记到地图上,然后所有用户都可以看到此信息。

我可以将我的数据上传到 Firebase,但是我很难将这些数据拉回以作为注释显示在地图上。

Firebase Data Structure

我创建了一个 loadCustomAnnotations() 来加载数据库信息作为我地图上的图钉,我知道我需要观察这些值并只加载到 "art" 路径中,然后遍历所有结果但是我似乎无法将其变成注释!谁能帮忙?!谢谢。

func loadCustomLocations() {

    FIRDatabase.database().reference(withPath: "art")

    let artPin = FIRDatabase.database().reference(withPath: "art")

    artPin.observe(.value, with: { snapshot in


        for item in snapshot.children {
            guard let snapshot = item as? FIRDataSnapshot else { continue }

            let ref = art(snapshot: snapshot)

            print(snapshot)

            self.artDrops.append(ref)

     //       self.makeAnnotation(artDrops: art)


        }
    })
}

}

此功能现在可以打印我的所有位置,我只需要知道如何将其转换为注释即可!

Printing Locations

只要您现在能够上传并取回所有位置,接下来的步骤就不会那么难了。只需使用 .addAnnotations 就可以了!

let artAnnotation = MKPointAnnotation()
            var artDrops = [CLLocation]()
            for location in artDrops{
            artAnnotation.coordinate = location.coordinate
            artAnnotation.title = "your location title"
            mapView.addAnnotations([artAnnotation])
            }