自定义MKAnnotationView swift:func映射视图不执行任何操作
我正在尝试使用MapKit在地图上设置图像而不是pin。我知道我必须设置自定义的自定义MKAnnotationView swift:func映射视图不执行任何操作,swift,mapkit,mkannotationview,Swift,Mapkit,Mkannotationview,我正在尝试使用MapKit在地图上设置图像而不是pin。我知道我必须设置自定义的MKAnnotationView。因此,我应该执行以下几行: func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { if !(annotation is CustomPointAnnotation) { return nil }
MKAnnotationView
。因此,我应该执行以下几行:
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
}
但它在我的代码中根本不起作用。我复制/粘贴了其中的所有代码,将名为1.png的图像添加到我的支持文件中。但地图显示的是一个红色的pin,而不是1.png图像
看起来整个mapView功能都没用了。我必须在viewDidLoad中调用它吗?我该如何让它被使用?还有,reuseId是用来做什么的?我应该把它叫哪里
感谢您的回答并祝您新年快乐。请确保MKMapView的委托已正确链接到实现该方法的ViewController 您可以在“viewwillappease”方法中这样做:mapView.delegate=self 或在InterfaceBuilder中: -确保已选择MapKit视图 -转到“连接检查器”
-在“Outlets”部分,将“delegate”属性拖放到实现委托方法的ViewController中您是否在视图控制器中将
MKMapView
的delegate
设置为self
?(或者在Interface Builder中设计此场景时,将代理设置为视图控制器?)谢谢大家!我花了整整一年的时间寻找答案。这张图片终于出现了。它现在非常巨大,但我会看看是否可以编辑框架。如果不能,我将编辑图片本身。谢谢。