Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 961
Rahul Sharma Avatar asked Apr 13 '16 06:04

Rahul Sharma


2 Answers

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);
         }
    }

}
like image 66
Bhavin Ramani Avatar answered Nov 12 '22 15:11

Bhavin Ramani


The process is you need to allow pin to dragable, and you will get that destination's lat long on didChangeDragState of MKMapView.

Try below code.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
{
    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ident"];
    pinView.draggable = YES;
    pinView.animatesDrop = YES;
    return pinView;
}
- (void)mapView:(MKMapView *)mapView
 annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
   fromOldState:(MKAnnotationViewDragState)oldState
{
    if (newState == MKAnnotationViewDragStateEnding)
    {
        droppedAt = annotationView.annotation.coordinate;
        NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
    }
}
like image 45
Jatin Chauhan Avatar answered Nov 12 '22 14:11

Jatin Chauhan