将数据从查询传递到mapView viewForAnnotation函数

时间:2015-11-30 22:53:43

标签: ios swift mkmapview mkannotation

我正在尝试在MKMapView上显示自定义引脚。这些引脚将具有自定义图像以及将显示值的UILabel。

我能够使用标签成功创建自定义引脚。现在标签显示静态值。我查询了来自后端服务(如parse)的数据,并保存了每个点的数据。这样,当用户点击某个点时,我可以在viewController中显示数据,但是我不知道如何将这个数据从我的查询方法传递到didSelectAnnotation和viewForAnnotation方法。

我还想将标签显示的静态值更改为从服务器查询的值。我尝试通过创建一个名为CustomPointAnnotation的类来完成此操作,该类继承自MKPointAnnotation并具有三个属性的初始化程序。这些属性是在查询期间设置的,因此如何在mapViewDidSelectAnnotationViewviewForAnnotation函数中访问这些属性,以便我可以根据需要使用这些数据。 (用于将viewController内的标签文本设置为该特定注释的属性)。

下面的图片显示了viewController以及我到目前为止所拥有的内容:

enter image description here

这是自定义点类:

class CustomPointAnnotation: MKPointAnnotation {

var price: String!
var streetName: String!
var ratingValue: Int!


init?(price: String, streetName: String, ratingValue: Int) {
    self.price = price
    self.streetName = streetName
    self.ratingValue = ratingValue
    super.init()
}

}

以下是我在viewDidLoad中运行的查询:

 func displayPoints() {
    let pointsQuery = PFQuery(className: "testLocation")
    let currentLocation = PFGeoPoint(location: locationManager.location)
    pointsQuery.whereKey("location", nearGeoPoint: currentLocation, withinMiles: 2)
    pointsQuery.findObjectsInBackgroundWithBlock { (points, error) -> Void in
        if error == nil {
            print("number of spots: \(points?.count)")
            let spots = points! as [PFObject]
            for pinPoint in spots {
                let point = pinPoint["location"] as! PFGeoPoint
                let price = String(pinPoint["price"])
                let ratingValue = pinPoint["rating"] as! Int
                let streetName = "Park Street, San Francisco CA"
                self.customAnnotation = CustomPointAnnotation(price: price, streetName: streetName, ratingValue: ratingValue)

                //// PRINT DATA OBTAINED FOR TESTING PURPOSES///////////////////////////////////////////////////////////
                print(self.customAnnotation.price)
                print(self.customAnnotation.streetName)
                print(self.customAnnotation.ratingValue)
                ///////////////////////////////////////////////////////////////////////////////////////////////////////


                self.customAnnotation!.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
                self.priceArray.append(pinPoint["price"])
                self.customAnnotation!.price = pinPoint["price"] as? String
                self.mapView.addAnnotation(self.customAnnotation!)


            }
        } else {
            JSSAlertView().danger(self, title: "something went wrong", text: "error: \(error)")
        }
    }
}

这里是didSelectAnnotationView:

    func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    //var anot: MKAnnotation

    if ((view.annotation?.isKindOfClass(MKUserLocation)) != nil){
        view.image = nil
    }

    for anot in mapView.annotations {
        print(mapView.annotations.count)
        let annotationView = mapView.viewForAnnotation(anot)
        if (annotationView != nil) {
            annotationView?.image = UIImage(named: "pin")
            priceLabel.textColor = UIColor.whiteColor()
        }
        //priceLabel.textColor = UIColor.blueColor()
        view.image = UIImage(named: "pinselected")
        print("image changed")

    }
}

最后是viewForAnnotation方法:

   func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

if annotation.isKindOfClass(MKUserLocation){
    return nil
}


    if !(annotation is CustomPointAnnotation) {
        print("all custom images added")
        return nil
    }

   let reuseID = "identifier"

var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID)
    if annotationView == nil {
        annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: reuseID, price: "13" )
        annotationView?.canShowCallout = false

    } else {
        annotationView?.annotation = annotation
    }


                                                                                                                //let cpa = annotation as! CustomPointAnnotation
                                                                                                                //let annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: nil, price: "11")
                                                                                                                //annotationView!.addSubview(priceLabel)
    annotationView?.annotation = annotation
    annotationView?.image = UIImage(named: "pin.png")
    return annotationView
}

1 个答案:

答案 0 :(得分:2)

您可以使用as运算符快速向下转换。在didSelectAnnotationView中,annotationView具有注释属性。您的自定义注释视图将自定义注释作为其注释属性,因此您可以尝试通过以下方式将其向下转换为子类:

if let annotation = view.annotation as? CustomPointAnnotation

假设可能,您将可以访问您的子类的属性。

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
        //var anot: MKAnnotation

    if ((view.annotation?.isKindOfClass(MKUserLocation)) != nil){
        view.image = nil
    }

    for anot in mapView.annotations {
        print(mapView.annotations.count)
        let annotationView = mapView.viewForAnnotation(anot)
        if (annotationView != nil) {
            annotationView?.image = UIImage(named: "pin")
            priceLabel.textColor = UIColor.whiteColor()
        }
        //priceLabel.textColor = UIColor.blueColor()


    }
    view.image = UIImage(named: "pinselected")
    if let annotation = view.annotation as? CustomPointAnnotation
    {
        self.priceLabel.text = annotation.price //for example
        //update the rest of your UI
    }

    print("image changed")
}

类似地,在viewForAnnotation中,您可以将MKAnnotation向下转换为CustomPointAnnotation,将MKAnnotationView向下转换为CustomAnnotationView。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

if annotation.isKindOfClass(MKUserLocation){
    return nil
}


    if !(annotation is CustomPointAnnotation) {
        print("all custom images added")
        return nil
    }

   let reuseID = "identifier"
   let cpa = annotation as! CustomPointAnnotation
   var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as! CustomAnnotationView
    if annotationView == nil {
        annotationView = CustomAnnotationView(annotation: cpa, reuseIdentifier: reuseID, price: cpa.price)
        annotationView?.canShowCallout = false

    } else {
        annotationView?.annotation = cpa
        annotationView?.price = cpa.price
    }

    annotationView?.image = UIImage(named: "pin.png")
    return annotationView
}

您的CustomAnnotationView应在通过实施价格的didSet设置其价格时更新其价格标签。