我試圖在MKMapView上顯示自定義引腳。這些引腳將具有自定義圖像以及將顯示值的UILabel。將查詢中的數據傳遞給mapView viewForAnnotation函數
我能夠用標籤成功創建自定義引腳。現在標籤顯示一個靜態值。我從後端服務(如解析)查詢數據,並保存每個點的數據。這樣,當用戶點擊某個點時,我可以在viewController
中顯示數據,但是我不確定如何將這些數據從我的查詢方法傳遞到didSelectAnnotation和viewForAnnotation方法。
我也希望將標籤顯示的靜態值更改爲從服務器查詢的一個值。我試圖通過創建一個名爲CustomPointAnnotation
的類來實現此目的,該類繼承自MKPointAnnotation
並具有包含三個屬性的初始化程序。這些屬性是在查詢過程中設置的,那麼如何訪問mapViewDidSelectAnnotationView
和viewForAnnotation
函數中的這些屬性,以便我可以根據需要使用這些數據。 (用於將viewController
中的標籤文本設置爲該特定註釋的屬性)。
下面是顯示的viewController和我有什麼迄今爲止的圖像:
下面是用戶自定義點類:
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
}
非常感謝!像魅力一樣工作! – user3413380