【问题标题】:Custom annotations with same image具有相同图像的自定义注释
【发布时间】:2013-04-30 05:02:02
【问题描述】:

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

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

}

【问题讨论】:

    标签: iphone ios mkmapview mapkit mkannotation


    【解决方案1】:

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

    【讨论】:

    • 谢谢,我做了这个注释 *myAnno = [[Annotation alloc]init]; myAnno = 注释;然后 adDiscountLabel.text = [NSString stringWithFormat:@"%@%%",myAnno.adDiscountStr];这是正确的做法吗?
    • 第一行没有意义,因为您用第二行覆盖了该值。您应该首先检查注释是您期望的类,然后将其强制转换,如下所示:if([annotation isKindOfClass:[Annotation class]]) { Annotation *myAnno = (Annotation *)annotation; ... ... ... }
    【解决方案2】:

    只需在 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];
        }
    

    【讨论】:

    • 你的意思是你在 ios 6.1 上获得不同的折扣和图像,但在 ios 5.1 中没有??
    • 这里有一些地区,比如在 ios 5 上,您有使用谷歌地图的 mapkit,而在 ios 6 上,您有使用内置地图的 mapkit,可能存在差异——尤其是视觉方面
    猜你喜欢
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多