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
}
ご協力いただきありがとうございました。