2012-12-24 74 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網址找到了新的項目示例。