Reputation: 3647
I want to know how to update my annotation
title while i am dragging it because all the annotation
properties are get
only.
Code I have try:
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
}
}
Upvotes: 2
Views: 731
Reputation: 72410
In the drag method of MKMapViewDelegate
, annotation is read only so you can use didSelectAnnotationView
method of MKMapViewDelegate
because before drag it will call didSelectAnnotationView
method, for that declare one selectedAnnotation
instance property of type MKPointAnnotation
.
var selectedAnnotation: MKPointAnnotation?
Now use this property in didSelectAnnotationView
method.
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
selectedAnnotation = view.annotation as? MKPointAnnotation
}
Now to change the title use this annotation
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
}
}
Upvotes: 5