设置坐标时自定义MKA注释不移动 [英] custom MKAnnotation not moving when coordinate set
问题描述
我已经设置了自定义的MKA注释
I've got a custom MKAnnotation set up
class Location: NSObject, MKAnnotation {
var id: Int
var title: String?
var name: String
var coordinate: CLLocationCoordinate2D {
didSet{
didChangeValueForKey("coordinate")
}
}
当我设置坐标时,它不会在地图上移动图钉.我发现有人说这将迫使其更新,并在其中添加了带有关键字坐标的'didSet'.但没有,它不会动.我已经验证纬度/经度已更改并且正确,但销钉没有动.标题会更新并显示更改.
when i set the coordinate it does not move the pin on the map. I added the 'didSet' with the key word coordinate as i found people saying that would force it to update. but nothing, it doesn't move. i have verified the lat / long have changed and are correct, the pin just isn't moving. Title updates and displays the change.
我必须假设它是因为这是一个自定义注释.有帮助吗?
i have to assume its because this is a custom annotation. any help?
推荐答案
If you're going to manually post those change events, you have to call willChangeValue(forKey:)
in willSet
, too. In Swift 4:
var coordinate: CLLocationCoordinate2D {
willSet {
willChangeValue(for: \.coordinate)
}
didSet {
didChangeValue(for: \.coordinate)
}
}
或者在Swift 3中:
Or in Swift 3:
var coordinate: CLLocationCoordinate2D {
willSet {
willChangeValue(forKey: #keyPath(coordinate))
}
didSet {
didChangeValue(forKey: #keyPath(coordinate))
}
}
或者,更简单的是,只需将其指定为dynamic
属性,即可为您完成此通知.
Or, much easier, just specify it as a dynamic
property and this notification stuff is done for you.
@objc dynamic var coordinate: CLLocationCoordinate2D
对于Swift 2版本,请参见此答案的先前版本.
For Swift 2 version, see previous rendition of this answer.
这篇关于设置坐标时自定义MKA注释不移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!