【问题标题】:How to add image for annotation (mapView) using Firebase?如何使用 Firebase 添加注释图像(mapView)?
【发布时间】:2018-08-17 12:11:28
【问题描述】:

我有这个func 用于在mapView 上填充引脚:

    func obervePins(){
        let magRef = Database.database().reference().child("map")
        magRef.observe(.value) { (snapshot) in

            //var tempCoord = [CoordinatesOfMagazine]()

            for child in snapshot.children {
                if let chidSnapshot = child as? DataSnapshot,
                let dictMag = chidSnapshot.value as? [String: Any],
                let title = dictMag["Title"] as? String,
                let latitude = dictMag["Latitude"] as? String,
                    let longitude = dictMag["Longitude"] as? String {
//                let imageOfMagazine = dictMag["imageOfMagazine"] as? String,
//                let url = URL(string: imageOfMagazine) {

            let annotation = MKPointAnnotation()
                    annotation.coordinate = CLLocationCoordinate2D(latitude: Double(latitude)!, longitude: Double(longitude)!)
            annotation.title = title
                    print(annotation)
            self.mapView.addAnnotations([annotation])
//                    let coordinates = CoordinatesOfMagazine(imageOfMagazine: url, Title: title)
//                    tempCoord.append(coordinates)
                }
            }
            //self.coordinates = tempCoord
        }
    }

我在 Firebase 中的数据如下所示:

mapView 中的引脚是正确的。 我不知道,如何在 mapView 中显示杂志的图像。请帮忙

【问题讨论】:

    标签: swift firebase firebase-realtime-database mapkit


    【解决方案1】:

    创建一个自定义的MKAnnotation 类。

    class ImageAnnotation : NSObject, MKAnnotation {
        var coordinate: CLLocationCoordinate2D
        var title: String?
        var subtitle: String?
        var imageOfMagazine: String?
    
        override init() {
            self.coordinate = CLLocationCoordinate2D()
            self.title = nil
            self.subtitle = nil
            self.imageOfMagazine = nil
        }
    }
    

    设置数据并将注释添加到您的 mapView。

        let annotation = ImageAnnotation()
        annotation.coordinate = coordinate1
        annotation.title = "title"
        annotation.subtitle = "subtitle"
        annotation.imageOfMagazine = imageOfMagazine
        self.mapView.addAnnotation(annotation)
    

    实现mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation)委托方法。

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard let annotation = annotation as? ImageAnnotation else {
            return nil
        }
    
        let reuseId = "Pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if pinView == nil {
            pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView?.canShowCallout = true
            let data = NSData(contentsOf: URL(string: annotation.imageOfMagazine!)!)
            pinView?.image = UIImage(data: data! as Data)
        }
        else {
            pinView?.annotation = annotation
        }
    
        return pinView
    }
    

    【讨论】:

      【解决方案2】:
      // here take marker as global varible
      var marker  : GMSMarker? 
      
      self.ref.observe(.value) { snapshot in
      
          let dict = snapshot.value as! NSDictionary
          self.marker?.map?.clear()
      
          if let lat = dict["latitude"] as? CLLocationDegrees ,let long = dict["longitude"] as? CLLocationDegrees {    
      
              var camera = GMSCameraPosition.camera(withLatitude: lat, longitude: longt, zoom: 12)
              var position: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, longt)
              self.marker = GMSMarker(position: position)
              self.marker?.icon = UIImage(named: imageName) // set your image here
              self.marker?.map = self.mapview
              self.mapview?.animate(to: camera)
          }
      }
      

      【讨论】:

      • 这是答案还是问题?
      • 这是什么?我不明白
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多