The annotation pin is draggable but I couldn't drop it to the destination location. The problem is how could I get the destination location's coordinate where i drop the annotation pin? Any method exist?
Edit 1:
The center of the annotation pin seems never changed where ever I drop the pin.
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateEnding)
{
NSLog(@"x is %f", annotationView.center.x);
NSLog(@"y is %f", annotationView.center.y);
CGPoint dropPoint = CGPointMake(annotationView.center.x, annotationView.center.y);
CLLocationCoordinate2D newCoordinate = [self.mapView convertPoint:dropPoint toCoordinateFromView:annotationView.superview];
[annotationView.annotation setCoordinate:newCoordinate];
}
}
Edit 2:
Annotation.h
@property (nonatomic,readwrite,assign) CLLocationCoordinate2D coordinate;
Annotation.m
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
coordinate = newCoordinate;
}
Edit 3: where ever I drag the pin, the NSLog out put of the droppedAt is always the same.
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateEnding)
{
CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
}
}
Edit 4:
Annotaion.m @dynamic startAddr;
- (CLLocationCoordinate2D)coordinate
{
coordinate.latitude = [self.startAddr.latitude doubleValue];
coordinate.longitude = [self.startAddr.longitude doubleValue];
return coordinate;
}
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
//I need to update the self.startAddr.latitude and longitude here. Problem solved.
self.startAddr.latitude = [NSNumber numberWithDouble:newCoordinate.latitude];
self.startAddr.longitude = [NSNumber numberWithDouble:newCoordinate.longitude];
coordinate = newCoordinate;
}
First you have to create a custom annotation like this:
MyAnnotation.h
MyAnnotation.m
After that, you just have to use your annotation like this:
Also, you have to return a view for your annotation. You can do so like this:
Then, once you have adopted the
MKMapViewDelegate
protocol in your controller, you grab the coordinates of a pin after dragging it like this:Don't you wan't to do this :
Setting the annotation delegate, then
In your delegate :
Out of the blue, implement the setCoordinates method of your annotation and make a callBAck when your annotation coordinates have changed
edit : beware of the dragState property of your annotation while its moving.
Example for Swift 2.2, Xcode 7.3.1:
Create custom class adopting MKAnnotation protocol
(note: Swift doesn't have automatic notification for KVO-compliant properties, so I have added my own - this is not required):
Declare pin view as draggable:
Now, when the pin is dragged, it invokes the delegate method:
Here, you can retrieve the new end coordinates.
Alternatively, you could create a
userInfo
dictionary for the custom annotation'scoordinate
property's notifications. Observers could then retrieve the values upon receipt of the notification.In Swift my problem was that the
coordinate
attribute was alet
in my subclass and not avar
as it is in theMKAnnotation
.