Rahul Sharma Rahul Sharma - 1 month ago 9
iOS Question

How Can I Drag My Annotation On MKmap View Like Google Pin Drop And Draging:iOS

I need some help. How can i drag annotation (map pin) from one point to another in MKMapview.
Adding process is all good after pin drop-down on the map and i want to drag that pin (annotation) and change that place just like Google map.

Happy Coding

Answer

First allow pin to drag

- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id) annotation 
{ 
    pin.draggable = YES; 
}

- (void)mapView:(MKMapView *)mapView
 annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
   fromOldState:(MKAnnotationViewDragState)oldState
{
    if (newState == MKAnnotationViewDragStateEnding)
    {
        CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
        [annotationView.annotation setCoordinate:droppedAt];
         if(DEBUG_MODE){
        NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
         }
    }

}
Comments