如何使用Firebase为注释(mapView)添加图像?

时间:2018-08-17 12:11:28

标签: swift firebase firebase-realtime-database mapkit

我有这个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中的数据如下:

click

mapView中的引脚正确。 我不知道如何在mapView中显示杂志的图像。帮助plz

2 个答案:

答案 0 :(得分:0)

创建自定义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
}

答案 1 :(得分:-1)

// 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)
    }
}