2012-12-24 128 views

回答

3

要使注释可拖动,请将注释视图的可拖动属性设置为YES。

这通常在viewForAnnotation委托方法中完成,因此请确保您将MKMapView委托人设置为self,并将其与.h文件中的委托人相符。

例如:

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

好了,所以这里是管理注释拖动动作的代码:

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

这应该帮助!

4

你可以找到MKPinAnnotationView的源代码演示从this链接拖动&拖放功能。

更新:上述站点网址中给出的Github项目链接不起作用。但我从this网址找到了新的项目示例。