3 回答

TA貢獻1865條經驗 獲得超7個贊
在viewForAnnotation委托方法中,設置調用該方法所image依據的依據annotation。
一定要做到這一點后視圖被出隊或創建的(而不是只在if anView == nil部分)。否則,使用出隊視圖的注釋將顯示以前使用該視圖的注釋的圖像。
基本而言MKPointAnnotation,一種區分注釋的粗略方法是通過注釋,title但這不是很靈活。
更好的方法是使用實現MKAnnotation協議的自定義注釋類(一種簡便的方法是子類化MKPointAnnotation),并添加所需的任何屬性以幫助實現自定義邏輯。
在自定義類中,添加一個屬性,例如imageName,您可以使用該屬性基于注釋自定義圖像。
此示例子類MKPointAnnotation:
class CustomPointAnnotation: MKPointAnnotation {
var imageName: String!
}
創建類型的注釋CustomPointAnnotation并設置其imageName:
var info1 = CustomPointAnnotation()
info1.coordinate = CLLocationCoordinate2DMake(42, -84)
info1.title = "Info1"
info1.subtitle = "Subtitle"
info1.imageName = "1.png"
var info2 = CustomPointAnnotation()
info2.coordinate = CLLocationCoordinate2DMake(32, -95)
info2.title = "Info2"
info2.subtitle = "Subtitle"
info2.imageName = "2.png"
在中viewForAnnotation,使用imageName屬性設置視圖的image:
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if !(annotation is CustomPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.canShowCallout = true
}
else {
anView.annotation = annotation
}
//Set annotation-specific properties **AFTER**
//the view is dequeued or created...
let cpa = annotation as CustomPointAnnotation
anView.image = UIImage(named:cpa.imageName)
return anView
}

TA貢獻2012條經驗 獲得超12個贊
借助Anna和Fabian Boulegue的iOS Swift代碼:
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.delegate = self
var info1 = CustomPointAnnotation()
info1.coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)
info1.title = "Info1"
info1.subtitle = "Subtitle"
info1.imageName = "flag.png"
var info2 = CustomPointAnnotation()
info2.coordinate = CLLocationCoordinate2DMake(26.862280, 75.815098)
info2.title = "Info2"
info2.subtitle = "Subtitle"
info2.imageName = "flag.png"
mapView.addAnnotation(info1)
mapView.addAnnotation(info2)
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
println("delegate called")
if !(annotation is CustomPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.canShowCallout = true
}
else {
anView.annotation = annotation
}
//Set annotation-specific properties **AFTER**
//the view is dequeued or created...
let cpa = annotation as CustomPointAnnotation
anView.image = UIImage(named:cpa.imageName)
return anView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
class CustomPointAnnotation: MKPointAnnotation {
var imageName: String!
}

TA貢獻1779條經驗 獲得超6個贊
需要將注釋向下轉換為CustomPointAnnotation。更改為let pickupCustomAnno = annotation as! CustomPointAnnotation
- 3 回答
- 0 關注
- 625 瀏覽
添加回答
舉報