Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 拖放带有动画的MKPinAnnotationView_Iphone - Fatal编程技术网

Iphone 拖放带有动画的MKPinAnnotationView

Iphone 拖放带有动画的MKPinAnnotationView,iphone,Iphone,我不知道这是怎么回事。我正在创建一个CLGeocoder,根据字符串值删除一个pin。我有这个: - (void)placeMarkFromString:(NSString *)address { CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *erro

我不知道这是怎么回事。我正在创建一个CLGeocoder,根据字符串值删除一个pin。我有这个:

- (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,它显示了红色的pin。但是,它不具有动画效果。我基本上希望能够删除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的子类。谢谢

首先,使用自定义注释对象(例如AddressAnnotation)时可能看不到注释视图的原因是未设置其坐标,因此它显示在0,0处

在placeMarkFromString方法中,代码设置了AddressAnnotation的许多属性,但没有设置坐标,因此注释不会出现在预期的位置

如果设置坐标,它将显示在预期位置,并与动画一起显示

关于使用MKPlacemark时为什么看不到动画的另一个问题:

默认情况下,地图视图将创建一个带有红色pin但AnimateDrop设置为“否”的MKPinAnnotationView

因此,在viewForAnnotation中,必须显式地为MKPlacemark执行此操作,就像为AddressAnnotation执行此操作一样

如果所有注释都将使用MKPinAnnotationView,而不是检查要创建的每个不同注释类,当注释类为MKUserLocation时,可以通过在方法顶部返回nil来翻转条件,然后在不进行任何类检查的情况下运行其余代码,即在所有其他情况下返回MKPinAnnotationView并将animatesDrop设置为YES