0

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

- (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

我不知道问题到底出在哪里。谁能帮我指出我在上面代码中的错误。

谢谢

4

1 回答 1

4

你能试一下吗

[lastAnnotation  lastObject]

代替

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

喜欢

[MapView removeAnnotation:[lastAnnotation  lastObject]];  
[lastAnnotation removeObject:[lastAnnotation  lastObject]];        
于 2012-04-20T05:51:59.963 回答