使用 MapKit 和 Swift,我试图显示一些注释,每个注释都有一个唯一的图像显示在 leftCalloutAccessoryView 中。我能够显示具有不同标题和副标题的注释;但是,为每个注释显示的图像是相同的图像 - 我无法弄清楚如何使每个图像都独一无二。非常感谢任何和所有的反馈/指导,以使其发挥作用。下面的代码(ps我知道我已经对图像进行了硬编码;我不确定如何遍历每个注释以显示适当的图像):
override func viewDidLoad() {
super.viewDidLoad()
mySearch.delegate = self
theMapView.delegate = self
var latAustin:CLLocationDegrees = 30.274751
var lngAustin:CLLocationDegrees = -97.739141
var latAustinDelta:CLLocationDegrees = 0.1
var lngAustinDelta:CLLocationDegrees = 0.1
var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latAustinDelta, lngAustinDelta)
var austinLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latAustin, lngAustin)
var theRegion:MKCoordinateRegion = MKCoordinateRegionMake(austinLocation, theSpan)
theMapView.setRegion(theRegion, animated: true)
// println(theMapView.userLocation.coordinate.latitude)
// println(theMapView.userLocation.coordinate.longitude)
for var i = 0; i < politicians.count; i++ {
var politician = MKPointAnnotation()
var politicianTotalFunding:String = politicians[i]["totalFunding"]! as String
politician.coordinate = CLLocationCoordinate2DMake(politiciansCoords[i]["lat"]! as CLLocationDegrees, politiciansCoords[i]["lng"]! as CLLocationDegrees)
politician.title = politicians[i]["name"]! as String
politician.subtitle = "Total Funding: \(politicianTotalFunding)"
theMapView.addAnnotation(politician)
}
}
// Delegate method called each time an annotation appears in the visible window
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.pinColor = .Red
var imageview = UIImageView(frame: CGRectMake(0, 0, 45, 45))
imageview.image = UIImage(named: politicians[0]["photo"])
pinView!.leftCalloutAccessoryView = imageview
pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as UIButton
} else {
pinView!.annotation = annotation
}
return pinView
}
再次感谢您的所有帮助!