2012-08-13 10 views
6

devo l'annotazione pronto ad andare, ma cercando di capire su come rendere trascinabile con il mio codice:iOS - MKMapView - trascinabili NOTE

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

Grazie in anticipo!

risposta

14

Per rendere trascinabile un'annotazione, impostare l'annotazione vistadraggable proprietà su YES.

Questo è normalmente eseguito nel metodo delegato viewForAnnotation.

Ad esempio:

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


Se è necessario gestire quando l'utente interrompe il trascinamento e cade l'annotazione, si veda:
how to manage drag and drop for MKAnnotationView on IOS?


Inoltre, l'oggetto di annotazione (la quello che implementa MKAnnotation) dovrebbe avere una proprietà impostabile coordinate. Si sta utilizzando la classe MKPointAnnotation che implementa setCoordinate in modo che la parte sia già stata gestita.

+0

Grazie mille! Sono nuovo per iOS Dev. –

+0

Sto usando lo stesso, ma non so perché non funziona, voi ragazzi avete qualche idea .... – guru