【发布时间】:2016-07-10 01:43:51
【问题描述】:
我正在创建一个允许将表情符号附加到位置的地图应用程序。目前,图片显示在库存苹果地图标注上方。
我希望隐藏股票图钉图像,同时仍显示用户选择的表情符号。这可能吗?
到目前为止,我有这段代码可以执行此操作:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
//let identifier = "MyPin"
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
let dictName = arrLocation[((annotation as? MyAnnotation)?.index)!]
let imgName = dictName.valueForKey("pin_img") as? String
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(imgName!)
if annotationView == nil
{
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: imgName)
annotationView!.canShowCallout = false
let name = dictName.valueForKey("name") as! String
let annot = MyAnnotation(titleName:name)
annotationView?.annotation = annot
}
else
{
annotationView!.annotation = annotation
}
let detailButton: UIButton = UIButton(type: UIButtonType.Custom)
// size and placement of emoji on map
detailButton.frame = CGRectMake(-34,-25,85,85)
detailButton.tag = ((annotation as? MyAnnotation)?.index)!
detailButton.addTarget(self, action:"emojiTap:", forControlEvents: UIControlEvents.TouchUpInside)
detailButton.setBackgroundImage(UIImage(named:(dictName.valueForKey("pin_img") as? String)!), forState: UIControlState.Normal)
annotationView!.addSubview(detailButton)
return annotationView
}
【问题讨论】:
-
在您上面的代码中,如果您没有成功地将注释视图出列,则创建一个,但将其
annotation分配为新的MyAnnotation。您应该使用传递给此方法的annotation。此外,在您创建pin_img的位置,如果您只设置image的image属性会更容易。
标签: swift annotations mkmapview mkpinannotationview