iOS版 - 的MKMapView - 可拖动的注解

问题描述:

我有注释蓄势待发,但试图找出如何使它可拖动我的代码: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]; 
} 

提前感谢!

要使注释可拖动,请将注释视图的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