从mapview中删除引脚时崩溃

时间:2012-04-20 05:47:09

标签: iphone objective-c ios5

我正在使用MapView,我有两个按钮。 1)centreButton:    此按钮将引脚注释放在当前地图的中心。    按下此按钮时,我将最后一个注释存储在NSMutable数组中。    然后从mapview中删除最后一个注释,并将一个引脚放在地图的中心    我为此部分所做的代码如下:     丢弃引脚的功能

- (void)PinDropwithlatitude:(double)lat longitude:(double)longi droptitle:(NSString *)title
{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = lat;
    theCoordinate.longitude = longi;

    MKCoordinateRegion region;
    region.center.latitude  = theCoordinate.latitude;
    region.center.longitude = theCoordinate.longitude;

    //Set Zoom level using Span
    MKCoordinateSpan span;
    span.latitudeDelta  =0.005;
    span.longitudeDelta =0.005;
    region.span = span;
    [MapView setRegion:region animated:YES];

    SetLat =lat;
    SetLong =longi;
    DDAnnotation *annotation = [[[DDAnnotation alloc] initWithCoordinate:theCoordinate addressDictionary:nil] autorelease];
    annotation.title = title;
    annotation.subtitle = [NSString    stringWithFormat:@"%f %f", annotation.coordinate.latitude, annotation.coordinate.longitude];
    [MapView addAnnotation:annotation];    
}

当我按下中心按钮时,我正在执行以下代码并将最后一个数组存储在注释中。

-(IBAction)CenterPressed:(id)sender
 {
        //40.439631,-3.698273 -spain centre        
        [lastAnnotation addObjectsFromArray:MapView.annotations];
        NSLog(@"last annotation array=%@",lastAnnotation);
        for (id annotation in [MapView annotations]) 
        {
            if ([annotation isKindOfClass:[MKUserLocation class]]) 
            {
                 continue;
            }
            [MapView removeAnnotation:annotation];
        }    
        [self PinDropwithlatitude:SetLat longitude:SetLong  
        droptitle:NSLocalizedString(@"Title", nil)];
 }

数组的日志显示了我           最后的注释你可以在下面看到::

       last annotation array=(
      "<+40.43963100,-3.69827300> +/- 0.00m",
        "<+40.43923187,-3.68722200> +/- 0.00m",
      "<+40.43792343,-3.67670774> +/- 0.00m",
      "<+40.43772888,-3.66711617> +/- 0.00m"
      )

2)UNDOButton:删除当前放置的注释并重做前一个注释,因为我已经从mapview中删除了注释,并使用以下代码重新编写我之前维护过的数组中的注释最后注释:

  -(IBAction)undoPressed:(id)sender
   {
         if ([lastAnnotation count]>0)
         {
              int countAnn = [lastAnnotation count];
              [MapView removeAnnotation:[lastAnnotation objectAtIndex:countAnn-1]];  
              //[MapView delete:[lastAnnotation objectAtIndex:countAnn-1]];
              [lastAnnotation removeObjectAtIndex:countAnn-1];        
              double latitude = [[[lastAnnotation objectAtIndex:[lastAnnotation count]-1] annotation]coordinate].latitude;
              double longitude = [[[lastAnnotation objectAtIndex:[lastAnnotation count]-1]annotation]coordinate].longitude;        
              NSLog(@"count = %d",[lastAnnotation count]);           
              [self PinDropwithlatitude:latitude longitude:longitude droptitle:NSLocalizedString(@"Title", nil)];
         }
   }

但是当我按下撤销按钮时,它会崩溃并出现以下错误

- [DDAnnotation注释]:无法识别的选择器发送到实例0x79b8f40

我不知道,究竟问题出在哪里。任何人都可以帮我在上面的代码中指出我的错误。

感谢

1 个答案:

答案 0 :(得分:4)

你可以尝试

吗?
[lastAnnotation  lastObject]

而不是

[[lastAnnotation objectAtIndex:[lastAnnotation count]-1]

[MapView removeAnnotation:[lastAnnotation  lastObject]];  
[lastAnnotation removeObject:[lastAnnotation  lastObject]];