2017-03-17 55 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將檢查它添加新的引腳之前添加此行。 –