2012-08-13 15 views
6

que tienen la anotación listo para ir, pero tratando de averiguar sobre cómo hacer que se pueda arrastrar con mi código:iOS - MKMapView - Anotaciones pueden arrastrar

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

Gracias de antemano!

Respuesta

14

Para realizar una anotación que pueden arrastrarse, establezcadraggable la propiedad de la vista de anotación -YES.

Esto normalmente se hace en el método delegado viewForAnnotation.

Por ejemplo:

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


Si tiene que manejar cuando el usuario deja de arrastrar y deja caer la anotación, ver:
how to manage drag and drop for MKAnnotationView on IOS?


Además, el objeto de anotación (el uno que implementa MKAnnotation) debe tener una propiedad configurable coordinate. Está utilizando la clase MKPointAnnotation que implementa setCoordinate, por lo que esa parte ya se ha solucionado.

+0

¡Muchas gracias! Soy nuevo en el desarrollo de iOS –

+0

Estoy usando lo mismo, pero no sé por qué no está funcionando ¿ustedes tienen alguna idea ... – guru

Cuestiones relacionadas