1

これがどのように機能するのか混乱しています。文字列値に基づいてピンをドロップする CLGeocoder を作成しています。私はこれを持っています:

- (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 のサブクラスではありません。ありがとう!

4

1 に答える 1

0

まず、カスタム注釈オブジェクト(つまり)を使用したときに注釈ビューが表示されないのAddressAnnotationは、そのオブジェクトcoordinateが設定されていない(つまり、0,0で表示されている)ためです。

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

を設定するcoordinateと、期待どおりの場所にアニメーションとともに表示されます。


MKPlacemark: を使用したときにアニメーションが表示されない理由に関する他の質問について

デフォルトでは、マップビューはMKPinAnnotationView赤いピンでを作成しますが、にanimatesDrop設定されていNOます。

したがって、では、の場合と同じようviewForAnnotationに、明示的にそれを行う必要があります。MKPlacemarkAddressAnnotation

すべてのアノテーションがを使用する場合MKPinAnnotationViewは、作成するアノテーションの異なるクラスごとにチェックする代わりにnil、アノテーションクラスが存在するときにメソッドの先頭に戻って条件を反転しMKUserLocation、残りのコードを実行せずに実行できます。クラスチェック(つまり、他のすべての場合にMKPinAnnotationViewanimatesDrop設定して戻る)。YES

于 2012-05-14T14:09:56.633 に答える