2013-09-24 34 views
0

我有一个注释引脚,用于显示当前可以拖动的搜索位置。这个注解只是一个普通的MKPointAnnotation,然后我改变了viewForAnnotation方法中的图像。这是好的,直到我开始拖动注释,然后注释更改为默认的红色针脚并丢失我设置的自定义图像。iOS 7注释引脚图像在引脚被拖动时发生变化

我创建并添加这样的注解:

MKPointAnnotation *userAnnotation = [[MKPointAnnotation alloc] init]; 
[userAnnotation setCoordinate:userCoordinate]; 
[userAnnotation setTitle:@"My Location"]; 
[userAnnotation setSubtitle:@"Drag to move - Press to reset"]; 

[self.mapView addAnnotation:userAnnotation]; 

然后我这样设置annotationView:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
.... 
[annotationView setImage:[UIImage imageNamed:@"locationpin"]]; 
[annotationView setDraggable:YES]; 
[annotationView.layer setZPosition:999]; 
return annotationView; 

我实现方法didChangeDragState:fromState:像这样:

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

    if (newState == MKAnnotationViewDragStateEnding) { 
     CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate; 
     self.mockLocation = [[CLLocation alloc] initWithLatitude:droppedAt.latitude longitude:droppedAt.longitude]; 

     NSLog(@"%@ mock location set to %f %f", [self class], droppedAt.latitude, droppedAt.longitude); 

     [self plotStations]; 
    } 
} 

我不记得这个问题在iOS 6上,但它肯定存在于iOS 7上。

那么,如何让它在拖动之前,之中和之后保留其图像?

回答

0

我刚刚也有这个问题。 我设法通过在viewForAnnotation上将“MKPinAnnotationView”更改为“MKAnnotationView”来解决此问题。

事情是这样的:

- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation 
{ 
    MKAnnotationView *pin = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:@"my_pin"]; 
    if (pin == nil) { 
     pin = [[MKAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier:@"my_pin"]; 

    } else { 
     pin.annotation = annotation; 
    } 

    pin.draggable = YES; 
    pin.image = [UIImage imageNamed:@"img_map_pin"]; 
    pin.selected = YES; 

    return pin; 
} 

不同的是,拖放不会显示针落地的动画。

希望它可以帮助别人。

+0

我设法重新创建默认动画。将可能的时候发布代码... –