0
設置註釋查看圖像我有以下代碼來設置引腳圖像基於陣列中的哪個對象被用來創建引腳。 問題是引腳沒有顯示引腳的正確圖像。設置註解MKMapView顯示錯誤的圖像
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MyPin"
if annotation is MKUserLocation {
return nil
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
var selectedStation = fuel()
for station in fuelStations {
if (annotation.title! == station.Name){
selectedStation = station
}
}
var imageName = ""
switch(selectedStation.Brand) {
case "MORRISONS" :
imageName = "fuel-morrisons"
break;
case "TEXACO" :
imageName = "fuel-texaco"
break;
case "SAINSBURY" :
imageName = "fuel-sainsbury"
break;
case "SHELL" :
imageName = "fuel-shell"
break;
case "BP" :
imageName = "fuel-bp"
break;
case "TESCO" :
imageName = "fuel-tesco"
break;
case "TESCO EXTRA" :
imageName = "fuel-tesco"
break;
case "ESSO" :
imageName = "fuel-esso"
break;
default : imageName = "NEWFuel"
}
print("selected station: " + selectedStation.Brand + " imagename: " + imageName)
let pinImage = UIImage(named: imageName)
let size = CGSize(width: 40, height: 45)
UIGraphicsBeginImageContext(size)
pinImage!.draw(in: CGRect(x:0, y:0, width:size.width, height:size.height))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
annotationView?.image = resizedImage
} else {
annotationView?.annotation = annotation
}
return annotationView
}
這些引腳與創建:
for station in self.fuelStations{
let myAnnotation: MKPointAnnotation = MKPointAnnotation()
myAnnotation.coordinate = CLLocationCoordinate2DMake(station.lat, station.lng);
myAnnotation.title = station.Name
myAnnotation.subtitle = station.Brand
DispatchQueue.main.async {
self.mapView.addAnnotation(myAnnotation)
}
}
的放出來的,應該正確工作的打印建議,錯誤的圖片怎麼過還是在錯誤的腳。
selected station: TOTAL imagename: NEWFuel
selected station: TESCO EXTRA imagename: fuel-tesco
selected station: BP imagename: fuel-bp
selected station: ESSO imagename: fuel-esso
selected station: GULF imagename: NEWFuel
selected station: ESSO imagename: fuel-esso
selected station: ESSO imagename: fuel-esso
selected station: TESCO EXTRA imagename: fuel-tesco
selected station: MURCO imagename: NEWFuel
selected station: BP imagename: fuel-bp
selected station: GULF imagename: NEWFuel
selected station: JET imagename: NEWFuel
selected station: ESSO imagename: fuel-esso
selected station: GULF imagename: NEWFuel
selected station: ASDA imagename: NEWFuel
selected station: BP imagename: fuel-bp
selected station: TESCO imagename: fuel-tesco
selected station: JET imagename: NEWFuel
selected station: SHELL imagename: fuel-shell
selected station: ESSO imagename: fuel-esso
selected station: ESSO imagename: fuel-esso
selected station: SHELL imagename: fuel-shell
selected station: ASDA imagename: NEWFuel
selected station: SHELL imagename: fuel-shell
selected station: BP imagename: fuel-bp
selected station: ESSO imagename: fuel-esso
selected station: ESSO imagename: fuel-esso
selected station: TOTAL imagename: NEWFuel
其實,我建議你應該把所有的邏輯從'viewForAnnotation:'完全移出並且放到一個自定義的MKAnnotationView子類中。把一個setter觀察者放在'annotation'屬性上,並在那裏做你的邏輯。示例:https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch21p744maps/ch34p1008maps/MyAnnotationView.swift – matt