2012-05-13 7 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の色を落とし、コールアウトとタイトル/サブタイトルを場所の名前と住所にする機能を望んでいます。これはGoogleマップと同様です。しかし、私はアニメーションを取得していませんでした。

私は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に表示されている)ということです。

メソッドでは、AddressAnnotationではなく、coordinateのプロパティが多く設定されているため、アノテーションが期待どおりに表示されません。

coordinateを設定すると、期待どおりの位置にアニメーションが表示されます。 MKPlacemarkを使用しているときに何のアニメーションを見ていない理由として、他の質問については


:デフォルトでは

を、マップビューは赤いピンでMKPinAnnotationViewを作成するが、NOからanimatesDropセットとなります。

したがって、と同じように、の場合は明示的にMKPlacemarkにする必要があります。

すべての注釈がMKPinAnnotationViewを使用する場合は、代わりにあなたが作成される注釈のそれぞれ別のクラスをチェックする、あなたは注釈クラスが、その後MKUserLocationとする場合の方法のトップにnilを返すことで、周りの状態を反転させることができますクラスチェックを行わずに残りのコードを実行します(つまり、MKPinAnnotationViewanimatesDropとし、それ以外の場合はYESに設定します)。

関連する問題