2013-04-30 57 views
1

正如你可以低於圖像中看到的所有註釋具有相同的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

這是地圖視圖的常見錯誤。重要的是要知道,viewForAnnotation獨立於您在製作註釋的connectionDidFinishLoading方法中調用。當iOS根據它所決定的順序決定需要它時調用viewForAnnotation。這就是爲什麼其中一個參數是它需要繪製的註釋。在您的代碼中,您正在計算adDiscount標籤基於anno這是您的循環中的最後一項。相反,你應該使用annotation

+0

謝謝,我做了這個註釋* myAnno = [[註解alloc] init]; myAnno =註解; 然後adDiscountLabel.text = [NSString stringWithFormat:@「%@ %%」,myAnno.adDiscountStr];這是正確的方式嗎? – 2013-04-30 09:19:30

+0

你的第一行沒有意義,因爲你用第二行覆蓋了這個值。您應該首先檢查註釋是您期望然後投射它的類,如下所示: 'if([annotation isKindOfClass:[Annotation class]]) {0}註釋* myAnno =(Annotation *)註釋; ... ... ... }' – Craig 2013-04-30 09:49:18

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]; 
    } 
+0

試了一下,依然面臨着同樣的問題 – 2013-04-30 05:25:27

+0

這是工作的罰款在iOS 6.1而不是在iOS 5.1 – 2013-04-30 05:33:08

+0

你的意思是你在iOS 6.1不同的折扣和形象,但不是在IOS 5.1? – 2013-04-30 05:37:38