Chr*_*ris 5 mapkit mkannotation swift
我很欣赏这个问题可能看起来很奇怪,但基本上我是在从后端数据库中提取坐标后向地图添加注释.添加的注释量因用户而异.
let details = Annotation(title: "\(userName)",
locationName: "",
coordinate: CLLocationCoordinate2D(latitude:convertLat!, longitude: convertlon!))
self.mapView.addAnnotation(details as MKAnnotation)
self.mapView.selectAnnotation(details, animated: true)
Run Code Online (Sandbox Code Playgroud)
我遇到的麻烦是我想以特定的间隔更新"详细信息"注释的坐标,但我无法访问"详细信息"注释,因为它当然超出了范围.
例如,是否可以通过其标题名称访问注释并相应地更改其坐标?
另一种方法是删除所有注释并使用更新的坐标重新创建它们,但这是我试图避免的.
值得注意的是,出于多种原因,我不能简单地在我的方法之外创建细节注释.
谢谢.
更新 所以我正在尝试一种略有不同的方法,如下所示:
for annotation in mapView.annotations as [MKAnnotation] {
if (annotation as AnyObject).title == "Title of annotation" {
annotation.title = "Change to something else"
}
}
Run Code Online (Sandbox Code Playgroud)
但是我无法更改标题,因为Swift告诉我这是一个"只获取"属性,我不明白为什么.
将注释添加到mapView:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// if (annotation is MKUserLocation) { return nil }
let reuseID = "icon"
var v = mapView.dequeueReusableAnnotationView(withIdentifier: reuseID)
if v != nil {
v?.annotation = annotation
} else {
v = MKAnnotationView(annotation: annotation, reuseIdentifier: nil)
v?.image = snapShotImage
v?.canShowCallout = true
}
return v
Run Code Online (Sandbox Code Playgroud)
}
在不知道保存坐标的源类的结构的情况下,您可以创建一个包含源对象和创建的 MKAnnotation 的类。这个类将使用 KVO viaaddObserver来观察源坐标的任何变化,然后将它们应用到注释:对于标题/字幕也可以做同样的事情。像平常一样将创建的注释添加到地图中。确保拥有mapView的视图控制器保留上述类的实例,这样ARC就不会回收所有内容!
假设您仍然可以访问和更新源,UI 将自行处理。
| 归档时间: |
|
| 查看次数: |
962 次 |
| 最近记录: |