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 关注
- 600 浏览
添加回答
举报