具有相同图像的自定义注释

时间:2013-04-30 05:02:03

标签: iphone ios mkmapview mapkit mkannotation

正如您在下图中看到的,所有注释都具有相同的imagediscount不正确,它们应该具有不同的imagediscount。我怎样才能解决这个问题?我正在使用带有discountStr和'imgPathStr'属性的自定义注释类以及以下代码。 enter image description here

-(void) connectionDidFinishLoading: (NSURLConnection *) connection
{
     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

_adResultDict = [NSJSONSerialization JSONObjectWithData:_adResultData options:kNilOptions error:nil];

NSMutableArray *annoArray = [[NSMutableArray alloc]init];

NSArray *adArray = [[_adResultDict objectForKey:@"result"]objectForKey:@"ads"];

for (int i = 0; i<adArray.count;i++ ) {
    NSString *latLongStr = [[adArray objectAtIndex:i]objectForKey:@"area_lat_long"];
    NSArray *tempLatLongArray =[ latLongStr componentsSeparatedByString:@","];

    CLLocationCoordinate2D adPlace;
    adPlace.latitude = [[tempLatLongArray objectAtIndex:0] doubleValue];
    adPlace.longitude = [[tempLatLongArray objectAtIndex:1]doubleValue];
    anno = [[Annotation alloc]init];
    anno.title = [[adArray objectAtIndex:i]objectForKey:@"ad_title"];
    anno.adDiscountStr = [[adArray objectAtIndex:i]objectForKey:@"discount"];
    anno.adPurposeStr = [[adArray objectAtIndex:i]objectForKey:@"ad_tab"];
    anno.adImagePathStr = [[adArray objectAtIndex:i]objectForKey:@"image_name"];

    anno.coordinate = adPlace;
    [annoArray addObject:anno];


    }
 [searchMapView addAnnotations:annoArray];


 [searchMapView reloadInputViews];
}

委托方法:

 - (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>) annotation
{

MKAnnotationView *pinView = nil;
if(annotation != mapView1.userLocation)
{
    static NSString *defaultPinID = @"com.invasivecode.pin";
    pinView = (MKAnnotationView *)[mapView1 dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil )
        pinView = [[MKAnnotationView alloc]
                   initWithAnnotation:annotation reuseIdentifier:defaultPinID];

    //pinView.pinColor = MKPinAnnotationColorGreen;
    pinView.canShowCallout = YES;
    //pinView.animatesDrop = YES;
    UIImageView *adImageView = [[UIImageView alloc]initWithFrame:CGRectMake(8, 8, 69, 61)];
    adImageView.contentMode =UIViewContentModeScaleToFill;
    pinView.centerOffset = CGPointMake(0, -45);
    adImageView.backgroundColor =[UIColor clearColor];
    CALayer * l = [adImageView layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:7.0];

    UIImageView *adDiscountImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, -20, 75, 35)];
    adDiscountImageView.image = [UIImage imageNamed:@"map-price-tag"];
    UILabel *adDiscountLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 8, 40, 20)];
    //adDiscountLabel.center = adDiscountImageView.center;
    if ([anno.adDiscountStr isEqualToString:@""] ||[anno.adDiscountStr isEqualToString:@"0"]) {
        adDiscountLabel.text = anno.adPurposeStr;
    }
    adDiscountLabel.text = [NSString stringWithFormat:@"%@%%",anno.adDiscountStr];
    adDiscountLabel.backgroundColor =[UIColor clearColor];
    adDiscountLabel.textColor = [UIColor whiteColor];

    pinView.image = [UIImage imageNamed:@"map-pop-up.png"];
    [pinView addSubview:adImageView];
    [pinView addSubview:adDiscountImageView];
    [adDiscountImageView addSubview:adDiscountLabel];


//        
    }
    else {
        [mapView1.userLocation setTitle:@"Current Location"];

    }
    return pinView;

}

2 个答案:

答案 0 :(得分:2)

这是地图视图的常见错误。重要的是要知道viewForAnnotation是独立于您进行注释的connectionDidFinishLoading方法调用的。当iOS决定按照它决定的顺序需要它时,会调用viewForAnnotation。这就是为什么其中一个参数是需要绘制的注释。在您的代码中,您计算​​基于adDiscount 每个 anno标签,这是您循环中的最后一项。相反,您应该使用annotation

答案 1 :(得分:0)

只需在for循环中添加此波纹线即可。

[searchMapView addAnnotation: anno];

评论此轰鸣声......

[searchMapView addAnnotations:annoArray];

参见下面的示例..

    for (int i = 0; i<[arrJourneyData count]; i++) 
    {

            MyAnnotation *annTemp=[[MyAnnotation alloc]init];
            CLLocationCoordinate2D CLLTemp;
objectAtIndex:i] floatValue],[[[arrJourneyData valueForKey:@"journeylong"] objectAtIndex:i] floatValue]);
            CLLTemp.latitude = [[[arrJourneyData valueForKey:@"journeylat"] objectAtIndex:i] floatValue];
            CLLTemp.longitude = [[[arrJourneyData valueForKey:@"journeylong"] objectAtIndex:i] floatValue];
            annTemp.coordinate = CLLTemp;
            annTemp.title = [NSString stringWithFormat:@"%d Image of %@",totalrecord,[appDelegate getAddressFromLatLon:[[[arrJourneyData valueForKey:@"journeylat"] objectAtIndex:i] floatValue] withLongitude:[[[arrJourneyData valueForKey:@"journeylong"] objectAtIndex:i] floatValue]]];
            annTemp.anntag = i;
            [mapView addAnnotation:annTemp];
            [annTemp release];
    }