如何从下面的方法获取新的位置

时间:2013-03-04 06:48:30

标签: iphone ios mapkit mkannotation

-(void)mapView:(MKMapView *)mapView 
    annotationView:(MKAnnotationView *)view                  
    didChangeDragState:(MKAnnotationViewDragState)newState 
    fromOldState:(MKAnnotationViewDragState)oldState

2 个答案:

答案 0 :(得分:6)

你使用这个:

- (void)mapView:(MKMapView *)mapView 
        annotationView:(MKAnnotationView *)annotationView 
        didChangeDragState:(MKAnnotationViewDragState)newState 
        fromOldState:(MKAnnotationViewDragState)oldState 
{
    if (newState == MKAnnotationViewDragStateEnding)
    {
        CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
        NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
    }
}

这将为您提供MKAnnotationView被删除时最终位置的坐标。

答案 1 :(得分:0)

您可以检查拖动状态,然后使用annotationView.annotation.coordinate.latitudeannotationView.annotation.coordinate.latitude,您将获得新位置的坐标。

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState
{

    //if dragging ended
    if (newState == MKAnnotationViewDragStateNone && oldState == MKAnnotationViewDragStateEnding) {

        MKCoordinateRegion region;
        MKCoordinateSpan span;

        span.latitudeDelta = 0.00212872;
        span.longitudeDelta = 0.00212872;

        CLLocationCoordinate2D location;
        location.latitude = annotationView.annotation.coordinate.latitude;
        location.longitude = annotationView.annotation.coordinate.longitude; 
        [self getAddress:location.latitude withLong:location.longitude];

        region.span = span;
        region.center = location;

        if (addAnnotation != nil) {
            [myMap removeAnnotation:addAnnotation];
            [addAnnotation release];
            addAnnotation = nil;
        }

        addAnnotation = [[Annotation alloc] init];
        addAnnotation.coordinate = location;
        [myMap addAnnotation:addAnnotation];
        [myMap setRegion:region animated:YES];
        [myMap regionThatFits:region];
    }

}

addAnnotation是Annotation的对象。