在 Firebase 数据库中的 MKMapView 上放置多个标记

Posted

技术标签:

【中文标题】在 Firebase 数据库中的 MKMapView 上放置多个标记【英文标题】:Place multiple markers on MKMapView from Firebase Database 【发布时间】:2018-05-14 03:17:27 【问题描述】:

我正在创建一个 ios 应用程序,它使用 Apple 地图来显示当地车库/庭院销售的标记。到目前为止,我已经能够弄清楚如何从 Firebase 数据库中将一个标记放置在 Apple 地图上,但我不确定如何使用多个标记进行操作。我已经使用单元格完成了类似的任务,以在 Firebase 数据库的 UITableView 中显示不同的内容,但这是我第一次按地图进行操作。我正在阅读一篇文章 here,该文章展示了 JSON 数据如何实现这一点,但由于标记信息将是实时的,因此我的应用程序不可能采用这种方式。将多个标记添加到 MKMapView 的最佳方法是什么?

来自 ViewController 的片段(仅设置一个标记)

Database.database().reference().child("posts").observeSingleEvent(of: .value, with:  (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] 
            let artwork = Artwork(title: dictionary["title"] as! String,
                                  locationName: dictionary["location"] as! String,
                                  discipline: dictionary["category"] as! String,
                                  coordinate: CLLocationCoordinate2D(latitude: dictionary["lat"] as! Double, longitude: dictionary["long"] as! Double))
            self.mapView.addAnnotation(artwork)
        
    )

Artwork.swift

class Artwork: NSObject, MKAnnotation 
    let title: String?
    let locationName: String
    let discipline: String
    let coordinate: CLLocationCoordinate2D

    init(title: String, locationName: String, discipline: String, coordinate: CLLocationCoordinate2D) 
        self.title = title
        self.locationName = locationName
        self.discipline = discipline
        self.coordinate = coordinate

        super.init()
    

    var subtitle: String? 
        return locationName
    

【问题讨论】:

【参考方案1】:

感谢link@kosuke-ogawa 的发布,我能够找到解决方法。

ViewController.swift 中的代码片段

override func viewDidLoad() 
        super.viewDidLoad()    
        let ref = Database.database().reference()
        let postsRef = ref.child("posts")
        postsRef.observeSingleEvent(of: .value, with:  (snapshot) in
            for snap in snapshot.children 
                let postSnap = snap as! DataSnapshot
                if let dict = postSnap.value as? [String:AnyObject] 
                    let title = dict["title"] as! String
                    let locationName = dict["location"] as! String
                    let discipline = dict["category"] as! String
                    let coordinate = CLLocationCoordinate2D(latitude: dict["lat"] as! Double, longitude: dict["long"] as! Double)
                    let artwork = Artwork(title: title, locationName: locationName, discipline: discipline, coordinate: coordinate)
                    self.mapView.addAnnotation(artwork)
                
            
        )
    

如果有更简洁的方法可以做到这一点,请随时编辑我的代码以帮助将来的其他人。

【讨论】:

【参考方案2】:

对于 MKMapView 上的多个位置标记,您可以获取您创建的 ArtWork 数组,请尝试以下解决方案

var arrArtworks: [Artwork] = []

override func viewDidLoad() 
    super.viewDidLoad()
    intializeData()
    PlaceMarker()


func intializeData()
    Database.database().reference().child("posts").observeSingleEvent(of: .value, with:  (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] 
            let artwork = Artwork(title: dictionary["title"] as! String,
                                  locationName: dictionary["location"] as! String,
                                  discipline: dictionary["category"] as! String,
                                  coordinate: CLLocationCoordinate2D(latitude: dictionary["lat"] as! Double, longitude: dictionary["long"] as! Double))
            arrArtworks.append(Artwork)
        
    )


func PlaceMarker() 
    mapView.addAnnotations(arrArtworks)

【讨论】:

【参考方案3】:

例如可以使用for循环:

Database.database().reference().child("posts").observeSingleEvent(of: .value, with:  (snapshot) in
  for child in snapshot.children.allObjects as! [DataSnapshot] 
    if let dictionary = child.value as? [String: AnyObject] 
        let artwork = Artwork(title: dictionary["title"] as! String,
                              locationName: dictionary["location"] as! String,
                              discipline: dictionary["category"] as! String,
                              coordinate: CLLocationCoordinate2D(latitude: dictionary["lat"] as! Double, longitude: dictionary["long"] as! Double))
        self.mapView.addAnnotation(artwork)
    
  
)

参见。 http://takeip.com/swift-3-firebase-to-mapkit.html

【讨论】:

感谢您的回复,我该如何添加标记ID?这就是我的数据的结构i.imgur.com/5lpEPWY.png

以上是关于在 Firebase 数据库中的 MKMapView 上放置多个标记的主要内容,如果未能解决你的问题,请参考以下文章

Flutter Firebase:Firebase 中的数据更新,但未在屏幕上自动显示计数器更新

Android中的Firebase数据描述排序

在 Flutter 中的 ListView 中搜索(使用 Firebase 中的数据)

获取存储在 firebase 中的数据值

访问 Firebase 数据库中的多个节点

如何使用 Cloud Functions for Firebase 更新 Firebase 实时数据库中的值