Implementing MKPinAnnotationView drag feature?

半城伤御伤魂 提交于 2019-12-12 03:35:19

问题


Can anyone suggest a good tutorial for implementing dragging feature for MKPinAnnotationView in iPhone MKMapView?


回答1:


you can find source code demo for MKPinAnnotationView Drag & drop feature from this link.

Update: The Github project link given in above site url is not working. But I found new project example for that from this url.




回答2:


To make an annotation draggable, set the annotation view's draggable property to YES.

This is normally done in the viewForAnnotation delegate method so make sure you set the MKMapView delegate to self and conform to it in the .h file.

For example:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString *reuseId = @"pin";
    MKPinAnnotationView *pav = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (pav == nil)
    {
        pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
        pav.draggable = YES; // Right here baby!
        pav.canShowCallout = YES;
    }
    else
    {
        pav.annotation = annotation;
    }

    return pav;
}

Ok, so here is the code to manage a annotations drag action:

- (void)mapView:(MKMapView *)mapView 
    annotationView:(MKAnnotationView *)annotationView 
    didChangeDragState:(MKAnnotationViewDragState)newState 
    fromOldState:(MKAnnotationViewDragState)oldState 
{
    if (newState == MKAnnotationViewDragStateEnding) // you can check out some more states by looking at the docs
    {
        CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
        NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
    }
}

This should help!



来源:https://stackoverflow.com/questions/14017150/implementing-mkpinannotationview-drag-feature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!