1
votes

I have one calendar on map. Initially when I open view controller, all annotations of current date are shown. and I am getting these annotations' coordinates from web services. So, when I change date from calendar, I have to call web services and I get new coordinates of selected date. My problem is, I am adding annotations of current date. my code is

timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(addannotationwithtimerInitial:) userInfo:nil repeats:YES];
-(void) addannotationwithtimerInitial:(id)sender{
img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 60)];
if ([dateText stringForKey:@"dateTxt"] !=nil) {
     lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 60, 40)];
} else {
    lbl = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 60, 40)];
}

if (annoCount< _lat.count-1) {
    NSLog(@"anncount %d",annoCount);
    double lat = [[_lat objectAtIndex:(annoCount)] doubleValue];
    double longt = [[_longitude objectAtIndex:(annoCount)] doubleValue];
    //CLLocationCoordinate2D loc;
    loc.latitude = lat;
    loc.longitude = longt;
    NSLog(@"lac lat long %f %f",loc.latitude, loc.longitude);
    Annotation.title = [_divDate objectAtIndex:annoCount];
    NSLog(@" divv%@",[_divDate objectAtIndex:(annoCount)]);
    Annotation = [[MKPointAnnotation alloc] init];
    Annotation.coordinate = loc;
    [_mapView addAnnotation:Annotation];
}

if (annoCount == 0) {
    img.image = [UIImage imageNamed:@"ann1.png"];
    float spanX = 0.5;
    float spanY = 0.5;
    MKCoordinateRegion region;
    region.center.latitude = loc.latitude;
    region.center.longitude = loc.longitude;
    region.span.latitudeDelta = spanX;
    region.span.longitudeDelta = spanY;
    [self.mapView setRegion:region animated:YES];

}
else if (annoCount == _lat.count-1){
    img.image = [UIImage imageNamed:@"ann1.png"];
    [timer invalidate];

}
else{
    img.image = [UIImage imageNamed:@"ann2.png"];
}
annoCount++;  

}

This is adding perfectly. but when I change date, I am removing annotations and then again I am calling web services and showing annotations. but this time annotations are adding properly but values of annotations are not showing properly. It is showing previous values. my code is

[timer invalidate];
[_mapView removeAnnotation:Annotation];
for (id annotation in _mapView.annotations)
    if (annotation != _mapView.userLocation)
        [toRemove addObject:annotation];
[_mapView removeAnnotations:toRemove];
[self webservice];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self      selector:@selector(addannotationwithtimerInitial:) userInfo:nil repeats:YES];

and to show annotations on map method is

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
 {
   // If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;
static NSString *reuseId = @"reuseid";
av = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (av == nil)
{
    av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
   //img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 60)];

    [av addSubview:img ];
   // UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 60, 40)];
   // lbl1.text = [_divDate objectAtIndex:(annoCount)];
   // NSLog(@"lbl txt %@",lbl1.text);
    if ([dateText stringForKey:@"dateTxt"] !=nil) {
        lbl1.backgroundColor = [UIColor clearColor];
        lbl1.textColor = [UIColor whiteColor];
        [lbl1 setFont: [lbl1.font fontWithSize: 12]];
        [lbl1 setFont:[UIFont boldSystemFontOfSize:12]];
        lbl1.text = [_divDate objectAtIndex:(annoCount)];
        NSLog(@"lb tx %@",lbl1.text);
        lbl1.alpha = 1;
        [img addSubview:lbl1];

    } else {
        lbl.backgroundColor = [UIColor clearColor];
        lbl.textColor = [UIColor whiteColor];
        [lbl setFont: [lbl.font fontWithSize: 12]];
        [lbl setFont:[UIFont boldSystemFontOfSize:12]];
        lbl.text = [_divDate objectAtIndex:(annoCount)];
        lbl.alpha = 1;
        [img addSubview:lbl];

    }




    //Following lets the callout still work if you tap on the label...
    av.canShowCallout = YES;
    av.frame = lbl.frame;
}
else
{
    av.annotation = annotation;
}

lbl = (UILabel *)[av viewWithTag:42];
//NSLog(@"ann   %@",myAnnotation.title);

return av;

}

Please help me.

1

1 Answers

1
votes

Add this line before adding new pins in a map

[_mapView clear]

and u done