2015-11-30 19 views
0

我試圖在MKMapView上顯示自定義引腳。這些引腳將具有自定義圖像以及將顯示值的UILabel。將查詢中的數據傳遞給mapView viewForAnnotation函數

我能夠用標籤成功創建自定義引腳。現在標籤顯示一個靜態值。我從後端服務(如解析)查詢數據,並保存每個點的數據。這樣,當用戶點擊某個點時,我可以在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 
} 

回答

2

您可以向下投在迅速與作爲運營商。在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。當它的價格是通過實現價格的didSet設置

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應該更新它的價格標籤。

+0

非常感謝!像魅力一樣工作! – user3413380