具有相同图像的自定义注释 [英] Custom annotations with same image

查看:71
本文介绍了具有相同图像的自定义注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下面的图像所示,所有注释都具有相同的imagediscount,这是不正确的,它们应具有不同的imagediscount.我怎样才能解决这个问题?我正在使用具有discountStr和'imgPathStr'属性以及以下代码的自定义注释类.

As you can see in the image below that all annotations have same image and discount which is not correct, they should have different image and discount. How can I fix this? I am using a custom annotation class with discountStr and 'imgPathStr' property and the following code.

-(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;

}

推荐答案

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

This is a common mistake with map views. The important thing to know is that viewForAnnotation is called independently of the connectionDidFinishLoading method in which you are making your annotations. viewForAnnotation is called when iOS decides it needs it in the order it decides. That's why one of the parameters is the annotation it needs to draw. In your code you are calculating the every adDiscount label based on anno which is the last item in your loop. Instead you should be using annotation

这篇关于具有相同图像的自定义注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆