2012-08-13 57 views
6

我有註釋蓄勢待發,但試圖找出如何使它可拖動我的代碼:iOS版 - 的MKMapView - 可拖動的註解

-(IBAction) updateLocation:(id)sender{ 

    MKCoordinateRegion newRegion; 

    newRegion.center.latitude = mapView.userLocation.location.coordinate.latitude; 
    newRegion.center.longitude = mapView.userLocation.location.coordinate.longitude; 

    newRegion.span.latitudeDelta = 0.0004f; 
    newRegion.span.longitudeDelta = 0.0004f; 

    [mapView setRegion: newRegion animated: YES]; 


    CLLocationCoordinate2D coordinate; 
    coordinate.latitude = mapView.userLocation.location.coordinate.latitude; 
    coordinate.longitude = mapView.userLocation.location.coordinate.longitude; 

    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; 

    [annotation setCoordinate: coordinate]; 
    [annotation setTitle: @"Your Car is parked here"]; 
    [annotation setSubtitle: @"Come here for pepsi"]; 


    [mapView addAnnotation: annotation]; 
    [mapView setZoomEnabled: YES]; 
    [mapView setScrollEnabled: YES]; 
} 

提前感謝!

回答

14

要使註釋可拖動,請將註釋視圖的draggable屬性設置爲YES

這通常在viewForAnnotation委託方法中完成。

例如:

- (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; 
     pav.canShowCallout = YES; 
    } 
    else 
    { 
     pav.annotation = annotation; 
    } 

    return pav; 
} 


如果您需要在用戶停止拖動和下降的註釋來處理,請參閱:
how to manage drag and drop for MKAnnotationView on IOS?


此外,您的註釋對象(一個實現MKAnnotation)應該有一個可設置的coordinate屬性。您正在使用執行setCoordinateMKPointAnnotation類,因此該部分已經被處理。

+0

非常感謝!我是iOS開發新手。 – 2012-08-13 03:53:08

+0

我使用相同的,但不知道爲什麼它不工作你們有什麼想法.... – guru 2015-09-29 12:34:47