【问题标题】:Trouble creating custom MKAnnotationView创建自定义 MKAnnotationView 时遇到问题
【发布时间】:2017-08-24 21:21:47
【问题描述】:

我很难显示自定义注释视图。具体来说,我正在尝试将名为“pin”的图像设置为新的地图 pin。默认引脚始终显示。我已经做了几个小时的小改动,但没有成功,比如将“pin”更改为“pin.png”并更改 mapView:viewFor 方法的结构。这就是我所拥有的。请您看一下,看看有没有什么特别的地方?

感谢您的帮助!

注解类:

class Annotation: NSObject, MKAnnotation {
    dynamic var coordinate : CLLocationCoordinate2D
    var title: String?
    var subtitle: String?

    init(location coord:CLLocationCoordinate2D) {
        self.coordinate = coord
        super.init()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

AnnotationView 类:

class AnnotationView : MKAnnotationView {
    override init(annotation:MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation,
               reuseIdentifier: reuseIdentifier)
        let im = UIImage(named: "pin")!
        self.frame = CGRect(x: 0, y: 0, width: im.size.width / 3.0 + 5, height: im.size.height / 3.0 + 5)
        self.centerOffset = CGPoint(x: 0, y: -20)
        self.isOpaque = false
    }
    required init (coder: NSCoder) {
        fatalError("NSCoding not supported")
    }
    override func draw(_ rect: CGRect) {
        let im = UIImage(named: "pin")!
       im.draw(in: self.bounds.insetBy(dx: 5, dy: 5))
    }
}

mapView:viewFor: 方法:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    var v : MKAnnotationView! = nil
    let ident = "pin"
    v = mapView.dequeueReusableAnnotationView(withIdentifier: ident)
    if v == nil {
        v = AnnotationView(annotation: annotation, reuseIdentifier: ident)
        v.canShowCallout = true
    }
    v.annotation = annotation
    return v
}

其他相关方法:

@IBAction func submitDog(_ sender: Any) {
    let newDog = Dog(name: newDogName.text!, score: 11, picture: image!, location: location!)
    dogs.append(newDog)
    print(dogs.last!)
    UIView.animate(withDuration: 0.5, animations: {

    }) { _ in
        self.newDogView.animation = "slideUp"
        self.newDogView.animate()
        self.newDogView.isHidden = true
        self.newDogName.text = ""
        self.map.isUserInteractionEnabled = true
    }
    dropNewPin(locatedAt: dogs.last!.location, name: dogs.last!.name, rate: dogs.last!.score)
}

func dropNewPin(locatedAt: CLLocation, name: String, rate: Int) {
    let annotation = Annotation(location: CLLocationCoordinate2D(latitude: locatedAt.coordinate.latitude, longitude: locatedAt.coordinate.longitude))
    annotation.title = name
    annotation.subtitle = "\(rate)/10"
    self.map.addAnnotation(annotation)
}

【问题讨论】:

  • 您添加了self.mapView.delgate = self 吗?我没有看到那条线
  • 检查您的资产中是否有带有名称 pin 的图像,对我来说您的代码有效,但它不是正确的方法
  • @ReinierMelian 设置 mapview 委托有效!谢谢!然而,它引入了一些意想不到的行为。您所在位置的默认注释现在也设置为“pin”图像。
  • 检查我的答案

标签: ios swift mapkit mkannotationview mapkitannotation


【解决方案1】:

首先你需要添加你的viewController作为你的地图的代表

 self.mapView.delegate = self

之后我建议您使用MKAnnotationView 而不是使用自定义绘图修改和添​​加图像,如果您需要自定义注释视图,那么您需要添加一个 xib 文件和您的自定义类作为文件所有者并制作正确的调整

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    //to avoid make a custom Annotation view for your user location
    if(annotation is MKUserLocation){
        return nil
    }

    let ident = "pin"
    var v = mapView.dequeueReusableAnnotationView(withIdentifier: ident)
    if v == nil {
        v = MKAnnotationView(annotation: annotation, reuseIdentifier: ident)
        v?.image = UIImage(named: "pin")
        v?.canShowCallout = true
    }
    v?.annotation = annotation
    return v
}

【讨论】:

  • 明确一点,不需要自定义 MKAnnotationView 类?我应该使用自定义 MKAnnotation 类吗?我的下一步是自定义标注。
  • 如果您的自定义 annotationView 设计过于复杂,那么您需要制作自定义注释视图类,但如果您的 Annotation 设计只是一个图像,则不需要这样做@CodyPotter,自定义标注是另一个部分是一个单独的问题,MKAnnotation 是一个协议,实际上任何类都可以实现只有一个坐标属性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-07
  • 2013-04-21
  • 2011-12-27
  • 1970-01-01
相关资源
最近更新 更多