2017-03-17 56 views
1

我在地图上有一个日历。最初,当我打开视图控制器时,会显示当前日期的所有注释。我从Web服务获取这些注释的坐标。所以,当我从日历更改日期时,我必须调用Web服务,并获取所选日期的新坐标。我的问题是,我添加当前日期的注释。我的代码是地图上的注释查看

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

}

这是完全加入。但是当我更改日期时,我将删除注释,然后再次调用Web服务并显示注释。但是这次注释添加得很好,但注释的值没有正确显示。它显示了以前的值。我的代码是在地图上的方法

[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]; 

,并显示出注解是

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

}

请帮助我。

回答

1

在地图

[_mapView clear] 

,并没有做

+0

谢谢you..I将检查它添加新的引脚之前添加此行。 –