ank*_*kit 2 mapkit ios mapkitannotation swift2 swift3
我想知道如何annotation在拖动标题时更新我的标题,因为所有annotation属性都是get唯一的。
我尝试过的代码:
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
switch (newState) {
case .starting:
//view.dragState = .dragging
print("dragging.....")
case .ending, .canceling:
// view.dragState = .none
let lat = view.annotation?.coordinate.latitude
let long = view.annotation?.coordinate.longitude
let coordinates = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
print(" pin lat \(lat) long \(long)")
//Error here - title is get only property
view.annotation?.title = "New Location"
default: break
}
}
Run Code Online (Sandbox Code Playgroud)
在 的拖动方法中MKMapViewDelegate,注释是只读的,因此您可以使用 的didSelectAnnotationView方法,MKMapViewDelegate因为在拖动之前它会调用didSelectAnnotationView方法,为此声明一个selectedAnnotation类型的实例属性MKPointAnnotation。
var selectedAnnotation: MKPointAnnotation?
Run Code Online (Sandbox Code Playgroud)
现在在didSelectAnnotationView方法中使用这个属性。
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
selectedAnnotation = view.annotation as? MKPointAnnotation
}
Run Code Online (Sandbox Code Playgroud)
现在更改标题使用此注释
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
switch (newState) {
case .starting:
//view.dragState = .dragging
print("dragging.....")
case .ending, .canceling:
// view.dragState = .none
let lat = view.annotation?.coordinate.latitude
let long = view.annotation?.coordinate.longitude
let coordinates = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
print(" pin lat \(lat) long \(long)")
self. selectedAnnotation?.title = "New Location"
default: break
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
625 次 |
| 最近记录: |