1

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. 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];
}

Delegate method:

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

}
Ankur Arya
  • 4,693
  • 5
  • 29
  • 50

2 Answers2

2

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

Craig
  • 8,093
  • 8
  • 42
  • 74
  • thanks, I did this Annotation *myAnno = [[Annotation alloc]init]; myAnno = annotation; and then adDiscountLabel.text = [NSString stringWithFormat:@"%@%%",myAnno.adDiscountStr]; is this right way to do it? – Ankur Arya Apr 30 '13 at 09:19
  • There's no point in your first line since you overwrite the value with the second line. You should first check the annotation is the class you expect then cast it, like so: `if([annotation isKindOfClass:[Annotation class]]) { Annotation *myAnno = (Annotation *)annotation; ... ... ... }` – Craig Apr 30 '13 at 09:49
0

just add this bellow line inside the for loop ..

[searchMapView addAnnotation: anno];

comment this bellow line..

[searchMapView addAnnotations:annoArray];

See Example bellow..

    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];
    }
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • you mean you get different discount and image on ios 6.1 but not in ios 5.1?? – Paras Joshi Apr 30 '13 at 05:37
  • yes, see image https://www.dropbox.com/s/y1dfqemrmiyvqcy/iOS%20Simulator%20Screen%20shot%20Apr%2030%2C%202013%2011.10.41%20AM.png – Ankur Arya Apr 30 '13 at 05:42
  • here some region like on ios 5 you have mapkit using google maps and ios 6 you have mapkit using built-in maps , there are likely to be differences -- especially visual wise – Paras Joshi Apr 30 '13 at 05:46
  • @Ankur see the answer from this link may it will helpful to you... http://stackoverflow.com/questions/13869635/mkmapview-off-screen-annotation-image-incorrect :) if its helpful to you then please accept and up-vote the answer.. – Paras Joshi Apr 30 '13 at 05:49