2012-08-13 10 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プロパティを持つ必要があります。あなたはsetCoordinateを実装するMKPointAnnotationクラスを使用しているので、その部分はすでに処理されています。

+0

ありがとうございました!私はiOSデベロッパーが初めてです。 –

+0

私は同じものを使用していますが、なぜそれが動作していないのか分からない人はあなたのアイデアを持っている.... – guru

関連する問題