java的注解定义map_如何将自定义属性从Mapbox注释传递到其标注视图?

我将 MGLAnnotation 子类化为添加额外的属性 var tags ,如下所示:

class MasterMapAnnotation: NSObject, MGLAnnotation {

var coordinate: CLLocationCoordinate2D

var title: String?

var subtitle: String?

var tags: String?

init(coordinate: CLLocationCoordinate2D, title: String?, subtitle: String?, tags: String?) {

self.coordinate = coordinate

self.title = title

self.subtitle = subtitle

self.tags = tags

}

}

这是传入 Map 的注释:

let annotation = MasterMapAnnotation(coordinate: CLLocationCoordinate2D(latitude: 50.0, longitude: 50.0), title: "Numero Uno", subtitle: "Numero Uno subtitle", tags: "#tag1 #tag2")

mapView.addAnnotation(annotation)

mapView委托调用自定义标注视图:

func mapView(_ mapView: MGLMapView, calloutViewFor annotation: MGLAnnotation) -> MGLCalloutView? {

return MasterMapCalloutView(representedObject: annotation)

}

所以在callout视图子类(下面)中,如何访问这个新属性 tags ?

class MasterMapCalloutView: UIView, MGLCalloutView {

var representedObject: MGLAnnotation

let dismissesAutomatically: Bool = false

let isAnchoredToAnnotation: Bool = true

lazy var leftAccessoryView = UIView()

lazy var rightAccessoryView = UIView()

weak var delegate: MGLCalloutViewDelegate?

required init(representedObject: MGLAnnotation) {

self.representedObject = representedObject

super.init(frame: .zero)

}

required init?(coder decoder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

// callout view delegate: present callout

func presentCallout(from rect: CGRect, in view: UIView, constrainedTo constrainedView: UIView, animated: Bool) {

if !representedObject.responds(to: #selector(getter: MGLAnnotation.title)) {

return

}

view.addSubview(self)

let title = representedObject.title

let tags = representedObject.tags // how to access this?

}

}

问题显然是子类( MasterMapAnnotation )拥有自定义 tags 属性,而不是它的超类( MGLAnnotation ),但我不知道如何(a)将representObject设置为 MasterMapAnnotation 或(b)只是访问 MasterMapAnnotation 自定义属性而不重新配置协议 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值