2012-08-13 1 views
2

나는 드래그 할 수있는 MKAnnotationView을 가지고 있는데, 드래그의 시작과 끝에서 연속적이 아닌 콜백을받는 didChangeDragState: 대리자 메소드를 구현했습니다. 내가 끌고있는 주석의 현재 좌표를 추적하고 싶습니다. 제발 좀 도와주세요. 고맙습니다.MKAnnotationView를 끌 때 연속 호출을 얻는 방법

+0

이 방법을 사용해보십시오 : 가'- (무효)지도보기 (MKMapView *)지도보기 regionDidChangeAnimated : (BOOL)가 animated' – Dhruv

답변

0

는 annotationview.coordinates에서 가져온 할 수 있습니다 좌표 :

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
    didChangeDragState:(MKAnnotationViewDragState)newState fromOldState: 
    (MKAnnotationViewDragState)oldState 
    { 
    CLLocationCoordinate2D currentCoordinates = view.annotation.coordinate; 
    } 
+0

는이 방법 만 호출 될 때 주석 뷰의 드래그 상태가 한 상태에서 다른 상태로 변경되면 뷰가 드래그 될 때 연속적으로 호출되지 않습니다. 'Dragging' 상태로 바뀔 때 한 번만 호출됩니다. 또한 주석의 좌표는 상태가 '종료 됨'으로 바뀌면 업데이트되며 드래그하는 동안 업데이트되지 않습니다. – Ziewvater

0

는 지금까지 내가 아는 한,이 작업을 수행하기위한 애플에서 제공하는 방법이 아니라, 당신이 KVO를 통해 달성 할 수있다 (h/t = this answer).

MKAnnotationViewDragStateEnding을 입력 할 때까지 주석 좌표가 업데이트되지 않으므로 수동으로 주석보기 좌표를 결정해야합니다.

는 전체 솔루션은 다음과 같습니다

// Somewhere in your code before the annotation view gets dragged, subscribe your observer object to changes in the annotation view's frame center 
annotationView.addObserver(DELEGATE_OBJECT, forKeyPath: "center", options: NSKeyValueObservingOptions.New, context: nil) 

// Then in the observer's code, fill out the KVO callback method 
// Your observer will need to conform to the NSKeyValueObserving protocol -- all `NSObject`s already do 
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { 
    // Check that we're observing an annotation view. The `where` clause can be omitted if you're only observing changes in its frame, or don't care about extra calls 
    if let annotationView = object as? MKAnnotationView where keyPath == "center" { 
     // Defined below, `coordinateForAnnotationView` converts a CGPoint measured within a given MKMapView to the geographical coordinate represented in that location 
     let newCoordinate = coordinateForAnnotationView(pin, inMapView: self.mapView) 

     // Do stuff with `newCoordinate` 
    } 
} 

func coordinateForAnnotationView(annotationView: MKAnnotationView, inMapView mapView: MKMapView) -> CLLocationCoordinate2D { 
    // Most `MKAnnotationView`s will have a center offset, including `MKPinAnnotationView` 
    let trueCenter = CGPoint(x: annotationView.center.x - annotationView.centerOffset.x, 
     y: annotationView.center.y - annotationView.centerOffset.y) 

    return mapView.convertPoint(trueCenter, toCoordinateFromView: mapView) 
} 
관련 문제