如何根据json响应动态更改标注左附件图像视图

时间:2015-03-10 08:23:28

标签: ios objective-c mkmapview

我正在尝试根据json响应动态更改标注图像。但是只有一个图像显示所有标注请帮我怎么做

#pragma mark Map View Delegate Methods

注释视图的方法      - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation     {         //如果它是用户位置,则返回nil。         if([注释isKindOfClass:[MKUserLocation class]])             返回零;

    // Handle any custom annotations.
    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        // Try to dequeue an existing pin view first.
        MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
        if (!pinView)
        {
            // If an existing pin view was not available, create one.
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
            pinView.canShowCallout = YES;
            pinView.image = [UIImage imageNamed:@"pin.png"];
            pinView.frame = CGRectMake(0, 0, 25, 30);
            pinView.calloutOffset = CGPointMake(0, 32);

            // Add a detail disclosure button to the callout.
            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            pinView.rightCalloutAccessoryView = rightButton;

            NSLog(@"JSON: %@", islandArray); // print

            //to display a island image in callout view


            UIImageView *calloutImage = [[UIImageView alloc] init];

            NSLog(@"%@",[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"mapImage"]]);

            calloutImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"mapImage"]]]];

            calloutImage.frame = CGRectMake(10, 0, 30, 30);
            [calloutImage.layer setCornerRadius:05.0f];
            pinView.leftCalloutAccessoryView = calloutImage;
            pinView.tintColor = MKPinAnnotationColorRed;

        }
        else
        {
            pinView.annotation = annotation;
        }

        return pinView;
    }
    return nil;
}

0 个答案:

没有答案