【问题标题】:Place multiple markers on MKMapView from Firebase Database在 Firebase 数据库中的 MKMapView 上放置多个标记
【发布时间】: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
    }
}

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database mkmapview


    【解决方案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

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-04
        • 1970-01-01
        • 1970-01-01
        • 2018-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多