2012-05-13 82 views
1

我很困惑這是如何工作的。我正在創建一個CLGeocoder來根據字符串值刪除一個引腳。我有這個:用動畫刪除MKPinAnnotationView

- (void)placeMarkFromString:(NSString *)address { 
    CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
    [geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) { 
     [placemarks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
      NSLog(@"%@", [obj description]); 
     }]; 

     // Check for returned placemarks 
     if (placemarks && [placemarks count] > 0) { 
      CLPlacemark *topResult = [placemarks objectAtIndex:0]; 

      // Create an MKPlacemark and add it to the mapView 
      MKPlacemark *place = [[MKPlacemark alloc] initWithPlacemark:topResult]; 
      AddressAnnotation *anAddress = [[AddressAnnotation alloc] init]; 
      anAddress.address = place.subThoroughfare; 
      anAddress.street = place.thoroughfare; 
      anAddress.city = place.locality; 
      anAddress.state = place.administrativeArea; 
      anAddress.zip = place.postalCode; 
      anAddress.name = place.name; 

      //[self.mapView addAnnotation:place]; 
      [self.mapView addAnnotation:anAddress]; 
      self.currentPlacemark = place; 

      // Center map on that region 
      MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(topResult.location.coordinate, 2000, 2000); 
      MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:region]; 
      [_mapView setRegion:adjustedRegion animated:YES]; 
     } 
     else { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Results Found" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
      [alert show]; 
     } 
     if (error) { 
      NSLog(@"Error: %@", [error localizedDescription]); 
     } 
    }]; 
} 

所以最初,我把我的MKPlacemark添加到地圖上,它顯示紅色的引腳。但它不會生成動畫。我基本上希望能夠放棄任何3 MKPinAnnotationView顏色,有一個標註和標題/副標題是地方的名稱和地址,類似於谷歌地圖如何做。但我沒有得到任何動畫。

所以我想也許我需要創建我自己的符合MKAnnotation類的對象。所以我這樣做了,但是當我嘗試將它添加到位置時,我在viewForAnnotation委託方法中看不到它的annotationView。該方法是在這裏:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id<MKAnnotation>)annotation { 
    static NSString *placeMarkIdentifier = @"SimplePinIdentifier"; 
    if ([annotation isKindOfClass:[AddressAnnotation class]]) { 
     MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:placeMarkIdentifier]; 
     if (annotationView == nil) { 
      annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placeMarkIdentifier]; 
     } 
     else { 
      annotationView.annotation = annotation; 
     } 
     annotationView.enabled = YES; 
     annotationView.animatesDrop = YES; 
     annotationView.draggable = YES; 
     annotationView.pinColor = MKPinAnnotationColorPurple; 
     annotationView.canShowCallout = YES; 


     // Create a button for the annotation 
//  UIButton *rightArrowButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
//  annotationView.rightCalloutAccessoryView = rightArrowButton; 
//  [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:0.5]; 
     return annotationView; 
    } 
    return nil; 

} 

所以我想我的問題是,我需要創建自己的對象要做到這一點,我是在正確的軌道上,我究竟做錯了,爲什麼它在一種情況是,我添加一個MKPlacemark對象,然後如果以其他方式執行操作,我添加一個對象,但不一定是MKPlacemark的一個子類。謝謝!

回答

0

首先,當您使用自定義註釋對象(即AddressAnnotation)時,您可能未看到註釋視圖的原因是它的coordinate沒有被設置(因此它出現在0,0處)。

placeMarkFromString方法中,代碼設置了AddressAnnotation的許多屬性,但不是coordinate,因此註釋未出現在預期位置。

如果您設置了coordinate,它將出現在預期位置和動畫中。


至於其他的問題,爲什麼你使用MKPlacemark時,看不到動畫:

默認情況下,地圖視圖將創建一個MKPinAnnotationView與紅色的大頭針但animatesDrop設置爲NO

因此,在viewForAnnotation中,您必須明確地爲MKPlacemark這樣做,就像您對AddressAnnotation

如果你所有的註釋將使用的不是檢查每個不同的類,你將創建註釋MKPinAnnotationView,您可以通過在方法的頂部返回nil左右翻轉狀態時,註釋類爲MKUserLocation,然後在沒有任何類別檢查的情況下運行其餘的代碼(即,在所有其他情況下,返回MKPinAnnotationViewanimatesDrop設置爲YES)。