【问题标题】:Hide map pin annotation image隐藏地图图钉注释图像
【发布时间】: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


【解决方案1】:

自定义注释视图

不要使用 MKPinAnnotationView,而是使用它的超类 MKAnnotationView。

响应输入

要响应用户点击注释,请实现mapView:didSelectAnnotation: 委托方法。点击注解时会调用此委托方法。

请注意,您不需要使用注释视图中的按钮。这意味着emojiTap: 中的功能必须由mapView:didSelectAnnotationMethod: 实现。

示例

结合这些

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    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 = MKAnnotationView(annotation: annotation, reuseIdentifier: imgName)
        let name = dictName.valueForKey("name") as! String
        let annot = MyAnnotation(titleName:name)
        annotationView?.annotation = annot    
    }
    else
    {
        annotationView!.annotation = annotation
    }

    annotationView!.canShowCallout = false

    return annotationView
}

func mapView(_ mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {

    guard let annotation = view.annotation as? MyAnnotation else {
        return
    }

    print("tapped annotation: \(annotation.index)

    // Implement the code from emojiTap: here.
}

【讨论】:

  • 太好了,这会移除图钉!但是我现在似乎无法选择丢弃的表情符号:(有什么建议吗?
  • 这只是另一个MKMapViewDelegate 方法,因此它可以与您的mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) 进入同一个文件。
  • 我在尝试实现时遇到错误:( "'mapView(_didselectannotationview:)' 的重新声明无效"
  • 更改了func的名称,但仍然无法选择注释:(当用户在表情符号上按住手指时,将显示一个弹出视图
  • @Oscar 所有错误意味着您已经拥有didSelectAnnotationView。您可以只使用现有方法,而不是尝试添加另一种方法。我已经更新了答案以尝试更清楚地解释这一点。基本上不是使用emojiTap,而是使用didSelectAnnotationView。不需要注释视图内的按钮。如果这仍然没有意义,请随时继续聊天。
猜你喜欢
  • 1970-01-01
  • 2012-05-30
  • 2011-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-09
相关资源
最近更新 更多